Problem with seekg ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kapil Khosla

    Problem with seekg ?

    Hi,
    I am trying to move the file pointer back to the original location,
    but not able to using seekg, can someone help ?
    Thanks,
    Kapil
    Code:
    #include "stdafx.h"
    using std::cerr;
    using std::endl;
    using std::ios;
    #include <iostream>
    
    #include <fstream>
    using std::ifstream;
    using std::string;
    #include <sstream>
    #include <map>
    using std::map;
    using std::multimap;
    using std::cout;
    using std::pair;
    
    
    int main ( )
    {
    ifstream file_read("data.txt",ios::in);
    
    if(!file_read)
    {
    cerr << "File could not be opened\n" << endl;
    exit(1);
    }
    
    file_read.seekg(0);
    string inputString;
    
    cout << "File pointer is at " << file_read.tellg();
    
    while (file_read >> inputString)
    {
    }
    
    file_read.seekg(0);
    // Why seekg fails !!!
    cout << "File pointer is at " << file_read.tellg();
    
    
    }
  • srmoody
    New Member
    • May 2006
    • 1

    #2
    Originally posted by Kapil Khosla
    Hi,
    I am trying to move the file pointer back to the original location,
    but not able to using seekg, can someone help ?
    Thanks,
    Kapil
    Code:
    #include "stdafx.h"
    using std::cerr;
    using std::endl;
    using std::ios;
    #include <iostream>
    
    #include <fstream>
    using std::ifstream;
    using std::string;
    #include <sstream>
    #include <map>
    using std::map;
    using std::multimap;
    using std::cout;
    using std::pair;
    
    
    int main ( )
    {
    ifstream file_read("data.txt",ios::in);
    
    if(!file_read)
    {
    cerr << "File could not be opened\n" << endl;
    exit(1);
    }
    
    file_read.seekg(0);
    string inputString;
    
    cout << "File pointer is at " << file_read.tellg();
    
    while (file_read >> inputString)
    {
    }
    
    file_read.seekg(0);
    // Why seekg fails !!!
    cout << "File pointer is at " << file_read.tellg();
    
    
    }

    Try adding this before the last attempt to reset the pointer
    file_read.clear ();
    I had a similar problem and located a reason for it that made sense and fixed my problem. It seems that when you finished the last read it read the end of file which set the fail bit on the next read. When there are fail flags set the seekg will not work. Once you clear the flags (I just did a simple clear all clear(), then your seekg should work.

    Hope this helps you out, it took me awhile to track down the failure and solution.
    -Steve

    Comment

    • Oggan
      New Member
      • Jun 2006
      • 1

      #3
      Thanks

      thanks a lot.. i had the same problem and i googled for it and fortunatly ended up here =D

      Comment

      • kirsc001
        New Member
        • Jun 2006
        • 2

        #4
        Wow I have been searching for the answer to this ALL DAY! I resorted to closing and reopening the file several times in order to use seekg again! THANKS SO MUCH!

        Comment

        Working...