Archived

This topic is now archived and is closed to further replies.

Indy

fstream

Recommended Posts

Hi, If I use the fstream i/o lib, how do I scan for file errors? In particular when a file does not exist. I have looked everywhere in the MS VC++ help and cannot find a method. fstream f,mat; f.open("list.txt",ios::in); //MessageBox(hdwnd,result,"",MB_OK); if(ios::fail!=0)MessageBox(hdwnd,"W","e",MB_OK); while (!f.eof()) { f.getline(part[x],15,''\t''); f.getline(description[x],50,''\n''); x++; } ListLoad=true; f.close(); My code is as above. Thanks Indy

Share this post


Link to post
Share on other sites
Isn''t that it right there? Won''t the condition ios::fail != 0 fail if the file doesn''t exist?

Anyway, won''t this do the trick?

  
fstream f, mat;

f.open("list.txt", ios::in);
if(f.is_open())
{
while(!f.eof())
{
f.getline(part[x], 15, ''\t'');
f.getline(description[x], 50, ''\n'');
++x; // gee, I hope x is initialized somewhere =)

}

ListLoad = true;
f.close();
}
else
MessageBox(hwnd, "Load failed", "", MB_OK | MB_ICONEXCLAMATION);

Share this post


Link to post
Share on other sites