Thread: seekg() Not Working

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    25

    seekg() Not Working

    Hi,

    For some reason, the following piece of code won't rewind the cursor position in the file stream:

    Code:
    // C++ Statements
    
    std::ifstream FileStream;
    FileStream.open("MyFile");
    
    char c;
    
    while(!FileStream.eof()) {
    FileStream.get(c);
    }
    
    FileStream.seekg(0, std::ios::beg);
    
    if(!FileStream.eof()) {
    std::cout << "Not EOF" << std::endl;
    }
    else {
    std::cout << "EOF" << std::endl;
    }
    // More C++ Statements
    The output is "EOF," not "Not EOF."

    But the following piece of code does rewind the stream:

    Code:
    // C++ Statements
    
    std::ifstream FileStream;
    FileStream.open("MyFile");
    
    char c;
    
    while(!FileStream.eof()) {
    FileStream.get(c);
    }
    
    FileStream.close();
    FileStream.open("MyFile");
    
    if(!FileStream.eof()) {
    std::cout << "Not EOF" << std::endl;
    }
    else {
    std::cout << "EOF" << std::endl;
    }
    // More C++ Statements
    In this case, the output is "Not EOF."

    Can someone please tell me why the seekg() statement is not rewinding the stream position?

    Thanks in advance for your help.

    *****

    I am using g++ in FC5.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just curious, but did you actually try to read from the file in the first example? It is possible that a FileStream.clear() before the seekg would be sufficient for your code to work as expected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by laserlight
    Just curious, but did you actually try to read from the file in the first example? It is possible that a FileStream.clear() before the seekg would be sufficient for your code to work as expected.
    I'm quite shure you are right.
    Code:
    while(!FileStream.eof()) {
         FileStream.get(c);
    }
    reads the file until eof. now the file is in error state ( eof ) and seekg is ignored.
    clear() should help.
    Kurt

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    25
    FileStream.clear() worked like magiC++!

    Thanks a lot, folks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  2. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  3. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  4. seekg woos
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-26-2002, 08:51 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM