seekg() Not Working

This is a discussion on seekg() Not Working within the C++ Programming forums, part of the General Programming Boards category; Hi, For some reason, the following piece of code won't rewind the cursor position in the file stream: Code: // ...

  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
    21,448
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ZuK
    ZuK is offline
    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, 07: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

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