End of file problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: End of file problem

  1. #1
    Join Date
    Oct 2005
    Posts
    173

    End of file problem

    Ok, I'm having trouble with an end of file condition.

    My problem occurs when the last line of the text file is a return. Somehow it's triggering an excption that does not quite make sense to me.

    if the last line is just a return, the 2nd try block will generate an error.

    ERROR: ios_base::failbit set

    1) why isn't this a eofbit?
    2) the exceptions must be always be called? I tried commenting it out and when I did that, no errors were thrown and the program just continued on.
    (I tried opening a file that didn't exist....no errors thrown, it just kept reading blanks)
    3) is C++ execptions different than java? I check getline and it didn't mention what type of error it's supposed throw
    4) I assume the way I have the fin.exceptions defined, it can only accept the three types of errors listed?

    Code:
    ifstream fin;
    fin.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit );
    try{
        fin.open(filein.c_str()); 	
    }
    catch(ifstream::failure e){
        cout<< " ERROR: "<<e.what()<<endl;
        cout<<" Check path or include names, could not open: "<<filein<<endl;
        exit(0);
    }		 
    while (!fin.eof()){
        try{
            getline(fin,currentLineRead,'\n');
        }
        catch (ifstream::failure e){
             cout<< " ERROR: "<<e.what()<<endl;
        }
        do something....
    }
    Last edited by rssmps; 10-18-2005 at 03:03 AM.

  2. #2
    Join Date
    Oct 2005
    Posts
    173
    bump...

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    what compiler is this? If your on unix, sometimes it thinks a blank line is an end of file. I havent figured that one out (when it does this and when not). If its older visual studio, it may have compatibility problems (explained below).

    1) why isn't this a eofbit? (what is an eofbit??) end of file is a byte or more?

    2) you never *have* to do exception handling. But you may crash or fail to detect problems.

    3)They are supposed to be similar to java. your help *should* document any errors that a routine can throw.


    I know the older visual studio would create a file if none was there unless your called
    file.open(name, ios::nocreate);
    this is not standard behavior. It should crash if you try to use a file thats not there.

    Note that terminate() is considered a better exit(0).

    Thats all the wisdom I have right now -- Ive read that several times and still am not sure why its acting strange.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Actually, you don't need to use exception handling. I don;t think it's an EOF problem either. Simply check the return status of fin.open() or even use

    Code:
    if (fin) 
    {success} 
    else 
    {error}
    eof() is irrelevant in this case because you're only opening the file, not reading from it yet.
    My suspcicion is that the file name passed to open() is corrupt or incorrect:

    Code:
    fin.open(filein.c_str());
    Try to stor the filename in a temp const char * and see if it's a valid name at debug time. The call
    Code:
    result=fin.open(cptr);
    and check the value of result. That's all you need.
    Danny Kalev

  5. #5
    Join Date
    Oct 2005
    Posts
    173
    I'm using VS .net 2003.

    The file name is not the problem, because I read the file up to the last line ok.

    I solved the issue by just removing the 2nd try/catch..... my question is really more towards understanding who is throwing what and when.

    what happens at the end of a file? I'm thinking something like this:
    Code:
    first line of text
    ....
    last line of text(return marker)(eof marker)
    Code:
    first line of text
    ....
    last line of text(eof)
    when I did the try/catch around getline(), the error was generated in the first example yet it didn't for the second.

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    I'm not sure I understand your last question (the syntax in particular) but there' no need to use exceptions to detect the eof. The common idiom is
    while (! fin.eof())
    {
    fin>>data; //fin.read() etc.

    }
    Danny Kalev

Similar Threads

  1. SQL DMO Restores in VB?
    By ObiWan in forum VB Classic
    Replies: 3
    Last Post: 05-23-2006, 10:35 AM
  2. Help with VB code
    By Ana in forum VB Classic
    Replies: 5
    Last Post: 04-27-2005, 03:16 PM
  3. Access database with VB GUI
    By Emma in forum VB Classic
    Replies: 1
    Last Post: 06-30-2003, 07:47 AM
  4. Re: Could Someone help reply to JIM
    By DVanRoekel in forum VB Classic
    Replies: 1
    Last Post: 08-11-2002, 02:48 AM
  5. Visual Inheritance - Very Temperamental
    By Steve Hall in forum .NET
    Replies: 4
    Last Post: 04-04-2002, 09:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center