-
ifstream error message
I am attempting to read a filename from the user and then open the file, however,
if I press enter instead of typing a filename I get an error from the compiler:
#######################
Debug Assertion Failed!
...
Expression: *file != _T('\0')
...
#######################
It works unless the user presses enter without typing a filename, here is the code:
string filename;
getline (cin, filename);
ifstream fin(filename.c_str());
Visual C++.NET
Windows XP
THANKS!
-
One possible solution is to make sure string filename contains the filename and is not empty.
Kuphryn
-
How can i test whether the string is empty or not.
Also...if i put my 'ifstream fin(filename.c_str());' inside an 'if' statement, all other references to 'fin' generate errors because it is an undeclared identifier
Thanks,
quizkiwi
-
>How can i test whether the string is empty or not.
Code:
#include <iostream>
#include <string>
int main()
{
std::string filename;
std::getline ( std::cin, filename );
if ( !filename.empty() )
std::cout<< filename <<std::endl;
else
std::cerr<<"You must enter a filename"<<std::endl;
}
>if i put my 'ifstream fin(filename.c_str());' inside an 'if' statement, all other references to 'fin' generate errors
So don't do that. :rolleyes:
-
Thanks, that works good!
->"Crushing dreams at the speed of sarcasm." ....my kinda person!