Advertisement

fstream

Started by July 26, 2001 02:55 PM
1 comment, last by Indy 23 years, 6 months ago
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
Indy
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);  
Advertisement
fstream will equal NULL if the file was not opened

if tFile was an fstream, then you would put

if(!tFile)

to test if it opened

This topic is closed to new replies.

Advertisement