Problem eof()

This is a discussion on Problem eof() within the C++ Programming forums, part of the General Programming Boards category; Hi! I've got bit of a problem here: Code: ....... while(!sfs.eof()) { Efs.open("Elib.txt", ios::in); ....... Efs.seekg(0); cout<<Efs.tellg()<<endl; while(!Efs.eof()) { char* ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386

    Problem eof()

    Hi!
    I've got bit of a problem here:

    Code:
    .......
    while(!sfs.eof())
    {
      Efs.open("Elib.txt", ios::in);
      .......
      Efs.seekg(0);
      cout<<Efs.tellg()<<endl;
      while(!Efs.eof())
      {
        char* Ewrds = new char[20];
        Efs.getline(Ewrds, 20, '\n');
        if(!strcmp(wrdsrc, Ewrds))
        {
          found=true;
          break;
        }
      }
      Efs.close();
    		
      if(found)
      {
        cout<< "Found";
      }else
      {
        cout<< "Not Found";
      }
    }
    ......
    In the above code, when strcmp() returns zero and the inner while loop breaks before the Efs reachs it's eof, all seems to work fine. But when the inner loop iterate until the eof is found, in the next iteration of the outer loop, tellg() returns (-1) and the inner while’s efo check returns non-zero. Could anyone please point out how to fix this?
    Thanks.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    First of all you shpuld read this:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    and then this:
    http://cboard.cprogramming.com/showt...558#post444558

    And then we'll see what to do next!

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    ...before the Efs reachs it's eof, all seems to work fine.
    Efs is a stream object and when it hits eof, the eof error flag is set for the stream object. Closing the file doesn't reset the stream object's error flags. So, when you reopen the file, the stream object Efs still has the eof error flag set--essentially you are still at the end of the file. You can reset Efs using the clear() method:

    Efs.clear();
    Last edited by 7stud; 03-03-2005 at 04:14 AM.

  4. #4
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386
    Thanks 7stud. I will try that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 08:29 PM
  2. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 01:30 PM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21