read and store from unknown file name

C

carmelo

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(name); // 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;
}
 
J

Jim Langston

carmelo said:
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,
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(name); // 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
}
 
J

Juha Nieminen

That would be even better like this:

if(!indata)
{
std::cerr << "Could not open ";
std::perror(name.c_str());
exit(EXIT_FAILURE);
}
 
E

Erik Wikström

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".


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,

Might as well just open it here.

std::ifstream indata( name );

std::ifstream indata(name.c_str());

I can't believe that they missed this in the current standard, but at
least it is fixed in the next.
 
J

Jim Langston

Erik said:
std::ifstream indata(name.c_str());

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

how to make this programm with class???? 2
input output files 3
Code Help 14
strange read in of binary file 4
file read problem 6
Crossword 14
Crossword 2
reading from either cin or file 2

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top