Thread: Problem eof()

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    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
    715
    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,663
    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 geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    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, 07:29 PM
  2. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12: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, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM