ifstream::failbit problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SantaClaus
    New Member
    • Feb 2007
    • 18

    ifstream::failbit problem

    Hi all,
    Im getting an exception in the middle of reading a file and here is my code
    Code:
    std::ifstream inf;
    const int bufSize=1024;
    char tbuffer[bufSize];
    std::string filex = path; // may be C:\test.txt
    const char* stx = &filex[0];
    inf.open(stx,ios_base::in|std::ios::binary);
    if(!inf.good())
    {
    	inf.clear();
    	inf.close();
    	std::cout<<std::endl<<"Cant open file "<<std::endl;
    	exit(1);
    }
    inf.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );
    try 
    {
    	while(inf.good())
    		{
    		inf.getline(tbuffer,bufSize);
    		cout<<endl<<tbuffer<<endl;
    		::Sleep(50);
    		}
            	inf.clear();
    		inf.close();
    		delete inf;
    }
    catch(ifstream::failure e)
    {
    	cout<<endl<<"exception-->file exception"<<e.what()<<endl;
    	exit(1);
    }
    This code itself is in a loop and so, Im using the sleep function for some delay for my custom processing.
    Please help me out with this. I have no clue whats the problem.

    Thanks a lot.

    SC
    Last edited by sicarie; Apr 8 '07, 01:44 AM. Reason: Please use [code] and [/code] tags around your code.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Remove this:

    inf.exceptions ( ifstream::eofbi t | ifstream::failb it | ifstream::badbi t );

    and this:

    delete inf;

    Comment

    Working...