the function I use is something (I'm doing this from memory, so it's likely there are mistakes!), like this:
Code:
bool GetFileInfo ( string FileName, string &FileInfo )
{
ifstream in ( FileName.c_str() );
if ( in )
{
string Temp;
FileInfo.clear();
while ( getline( in, Temp ) )
{
FileInfo += Temp;
FileInfo += "\n";
}
in.close();
}
else
{
in.close();
return false;
}
in.close();
return true;
}
it's VERY handy if you have a program which uses a LOT of file IO. I did a Book/DVD/Music Catloguer the other day, and I think I used that at least 12 times. I have it as a bool so that I can have it in an if statement in my main program, like so:
Code:
string Destination;
if ( GetFileInfo( "MyFile.txt", Destination ) )
{
cout<< "the file is as follows: " << endl
<< Destination << '\n';
}
else cout<< "Error in opening the file";
instead of having to do the function millions of times.