Hi! I need to read and store data from a file, but I don't the name of
it; Is this code correct and is there a better way to do this thing??
int main()
{
cout << " type file name:"
char * name;
cin >name;
ifstream indata;
int num1; // variables for input values
int num2;
int num3;
indata.open(nam e); // opens the file
if(!indata) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
indata >num1;
indata >num2;
indata >num3;
} 4 7958
carmelo wrote:
Hi! I need to read and store data from a file, but I don't the name of
it; Is this code correct and is there a better way to do this thing??
Define "better".
It sounds like the filename is unknown at compile time, but known at run
time. There are a few ways you could get the name, a few include:
1. Accept filename as run time paramater. I.e.
myprog filename.ext
2. Accept the filename as user input
3. Accept the file through std::cin using OS piping.
4. Accept the filename through some other method (graphical input, stored in
a file, etc..)
It depends on what you mean by "better".
int main()
{
cout << " type file name:"
char * name;
cin >name;
This is an error. name is an uninitialied pointer to a character. Yet you
never allocated any memmory for it, so it points .. somewhere. Anywhere.
But not to anything valid. Most likely when the program hits the cin >>
name a run time error will occur as cin attempts to write to memory your
program doesn't "own". Better is:
std::string name;
std::getline( std::cin, name );
getline is better than >in this case because getline will allow a space,
>will stop when it hits the first space leaving the remainder in the
buffer.
ifstream indata;
Might as well just open it here.
std::ifstream indata( name );
int num1; // variables for input values
int num2;
int num3;
indata.open(nam e); // opens the file
So that line isn't needed anymore.
if(!indata) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
indata >num1;
indata >num2;
indata >num3;
}
You are not checking if the data is valid. Better is:
if ( ! indata >num1 >num2 >num3 )
{
std::cerr << "File dataformat error." << std:;endl;
exit( 1 );
}
// Data is good, proceed
}
--
Jim Langston ta*******@rocke tmail.com
Jim Langston wrote:
> if(!indata) { // file couldn't be opened cerr << "Error: file could not be opened" << endl; exit(1); }
That would be even better like this:
if(!indata)
{
std::cerr << "Could not open ";
std::perror(nam e.c_str());
exit(EXIT_FAILU RE);
}
On 2008-03-01 11:36, Jim Langston wrote:
carmelo wrote:
>Hi! I need to read and store data from a file, but I don't the name of it; Is this code correct and is there a better way to do this thing??
Define "better".
It sounds like the filename is unknown at compile time, but known at run
time. There are a few ways you could get the name, a few include:
1. Accept filename as run time paramater. I.e.
myprog filename.ext
2. Accept the filename as user input
3. Accept the file through std::cin using OS piping.
4. Accept the filename through some other method (graphical input, stored in
a file, etc..)
It depends on what you mean by "better".
>int main() { cout << " type file name:" char * name; cin >name;
This is an error. name is an uninitialied pointer to a character. Yet you
never allocated any memmory for it, so it points .. somewhere. Anywhere.
But not to anything valid. Most likely when the program hits the cin >>
name a run time error will occur as cin attempts to write to memory your
program doesn't "own". Better is:
std::string name;
std::getline( std::cin, name );
getline is better than >in this case because getline will allow a space,
>will stop when it hits the first space leaving the remainder in the
buffer.
> ifstream indata;
Might as well just open it here.
std::ifstream indata( name );
std::ifstream indata(name.c_s tr());
I can't believe that they missed this in the current standard, but at
least it is fixed in the next.
--
Erik Wikström
Erik Wikström wrote:
On 2008-03-01 11:36, Jim Langston wrote:
>carmelo wrote:
>>Hi! I need to read and store data from a file, but I don't the name of it; Is this code correct and is there a better way to do this thing??
Define "better".
It sounds like the filename is unknown at compile time, but known at run time. There are a few ways you could get the name, a few include: 1. Accept filename as run time paramater. I.e. myprog filename.ext 2. Accept the filename as user input 3. Accept the file through std::cin using OS piping. 4. Accept the filename through some other method (graphical input, stored in a file, etc..)
It depends on what you mean by "better".
>>int main() { cout << " type file name:" char * name; cin >name;
This is an error. name is an uninitialied pointer to a character. Yet you never allocated any memmory for it, so it points .. somewhere. Anywhere. But not to anything valid. Most likely when the program hits the cin >name a run time error will occur as cin attempts to write to memory your program doesn't "own". Better is:
std::string name; std::getline ( std::cin, name );
getline is better than >in this case because getline will allow a space, >will stop when it hits the first space leaving the remainder in the buffer.
>> ifstream indata;
Might as well just open it here.
std::ifstrea m indata( name );
std::ifstream indata(name.c_s tr());
I can't believe that they missed this in the current standard, but at
least it is fixed in the next.
Oh, yeah. I forgot that ifstream takes a char* and not a std::string. I
sure hope they fix it.
--
Jim Langston ta*******@rocke tmail.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Torrent |
last post by:
When Trying to Load an XSLT File with the XslTransform i got a rather annoying Exception being thrown "System.Xml.XPath.XPathException: XsltContext is needed for this query because of an unknown function."
It was annoying because I had checked created the whole document and tested it in Internet Explorer 6.0 and everything worked perfectly; no errors or warnings.
After doing research i found out that most often times the error that i was...
|
by: Raghu Gupta |
last post by:
Hi,
Actually i am facing some problem using quickbooks and presently i am
not so expert in using it.
I am using c# code to retrive the data from quickbooks.
it doesn't matter which language we are using.
So i have .iif file of employee list,so through code i have to read .iif
file and get the data out of it and store that data in database.
As i can use QuickBook only to import .iif file and from quickbook i can
|
by: Xinyi Yang |
last post by:
Hi,
I have to read information out of a file. The format will be
string1,string2,...,
string3,string4,...,
....
(the string sould not contain ' ' anyway)
the size of each string is uncertain. I plan to create an array to contain the pointers to the strings. Yet I don't know how to read a string out from the file any let a pointer point to it. I thought about using char *p=(int *) (malloc(n*(sizeof(char)))), but it means I have to first...
|
by: ranjeet |
last post by:
Hay Guys can you all suggest me the points on the below issue
Problem :
The thing is that I have the data some thing like this.
1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190, 21820,
1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902,
2070, 3025, 4333, 5854, 7805, 9231, 10597,
16047........................... soo onnnnnn
|
by: James Norton-Jones |
last post by:
Hi,
Am I trying to hold the data of a DataGrid in a label so that when the form
is reposted the DataGrid can be repopulated. The problem I am having is that
I don't understand how to get the text into a stream in order to be able to
use DataSetOutcomes1.ReadXML(MyStream).
Thanks in advance,
James
| |
by: rudranee |
last post by:
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...lines.
i tried incrementing file pointer by 2 (fp=fp+2)
but it does'nt work
Can someone give me the code please.
|
by: |
last post by:
Hi,
I'm steel trying to read and update my XML file with Visual Basic
Express but i am unable to find the right way to read my xml file and
update it if neccessary...
Here is my problem : evry day, i store the number of children in my
classroom in my XML file. For exemple, on monday, my app ask me
something like this : msgbox ("Are the 28 children here today ?",vbyesno)
|
by: Brad King |
last post by:
Hello Everyone,
I am having trouble reading a file. My eventual plan is to read in the file, fix a formatting problem with a regex and export the changed data to a new file. However my importing skills are not working.
Can anyone help me in the right direction? I believe my problem is that I have set lengths for the import and what I should have done is look for the next space.
Can anyone show me a snippet that looks for the next...
|
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= |
last post by:
Hi
Is there any way to Read an INIFile from a string or Stream instead of a
physical file ???
I want to read the INIFile into a string then store in a db but when I read
the string from the db or save string to the db I don't want to have to copy
the string to a file to use the WritePrivateProfileString and
GetPrivateProfileString. Is there a way around this instead of writing my own
class to operate on a string or stream ???
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |