Do it more like this:
char fileName[30];
printf( "Enter filename: " );
scanf( "%s", fileName );
FILE *pFile;
pFile = fopen( fileName, "r" );
... etc
fclose( fFile );
Though technically this is C++ so you should use fstreams, really.
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
>technically this is C++ so you should use fstreams, really
Moreover, you should use std::string instead this unsafe C-style input of a single word (see %s format specification) in 30-byte buffer (the right way to nowhere):
std::string filename;
std::ifstream file;
cout << "Enter filename: ";
if (getline(cin,filename)) {
file.open(filename.c_str());
if (!file) {
std::cerr << "Can\'t open " << filename << std::endl;
return 2;
}
} else {
std::cerr << "Can\'t get filename" << std::endl;
return 1;
}
...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348