473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. using std::cerr;
  3. using std::endl;
  4. using std::ios;
  5. #include <iostream>
  6.  
  7. #include <fstream>
  8. using std::ifstream;
  9. using std::string;
  10. #include <sstream>
  11. #include <map>
  12. using std::map;
  13. using std::multimap;
  14. using std::cout;
  15. using std::pair;
  16.  
  17.  
  18. int main ( )
  19. {
  20. ifstream file_read("data.txt",ios::in);
  21.  
  22. if(!file_read)
  23. {
  24. cerr << "File could not be opened\n" << endl;
  25. exit(1);
  26. }
  27.  
  28. file_read.seekg(0);
  29. string inputString;
  30.  
  31. cout << "File pointer is at " << file_read.tellg();
  32.  
  33. while (file_read >> inputString)
  34. {
  35. }
  36.  
  37. file_read.seekg(0);
  38. // Why seekg fails !!!
  39. cout << "File pointer is at " << file_read.tellg();
  40.  
  41.  
  42. }
  43.  
Jul 19 '05 #1
3 13353
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
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. using std::cerr;
  3. using std::endl;
  4. using std::ios;
  5. #include <iostream>
  6.  
  7. #include <fstream>
  8. using std::ifstream;
  9. using std::string;
  10. #include <sstream>
  11. #include <map>
  12. using std::map;
  13. using std::multimap;
  14. using std::cout;
  15. using std::pair;
  16.  
  17.  
  18. int main ( )
  19. {
  20. ifstream file_read("data.txt",ios::in);
  21.  
  22. if(!file_read)
  23. {
  24. cerr << "File could not be opened\n" << endl;
  25. exit(1);
  26. }
  27.  
  28. file_read.seekg(0);
  29. string inputString;
  30.  
  31. cout << "File pointer is at " << file_read.tellg();
  32.  
  33. while (file_read >> inputString)
  34. {
  35. }
  36.  
  37. file_read.seekg(0);
  38. // Why seekg fails !!!
  39. cout << "File pointer is at " << file_read.tellg();
  40.  
  41.  
  42. }
  43.  

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
May 22 '06 #2
Oggan
1
thanks a lot.. i had the same problem and i googled for it and fortunatly ended up here =D
Jun 23 '06 #3
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!
Jun 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
2
by: Assertor | last post by:
Hi, All. (VC++6.0) I found some strange thins when using getline() and seekg() of std::ifstream. After the file position of an open file was shift to the end of the file, seekg() did not...
7
by: Michael C | last post by:
I need to read a text file, "rewind" it to the beginning and read it again. I've tried using fstream's seekg(0, ios_base::beg) method with no luck. For some reason it's still thinking it's at eof,...
1
by: pantatkau | last post by:
i am trying to use the seekg(offset, base) function to move the read position but cant seem to do it correctly. When i use tellg() to know if the seekg operation is successful or not , the tellg()...
11
by: Markus | last post by:
Hi, I want to get an integer from the user. If the user inserts a character instead of an int the program goes crazy. So I tried something like this: This program works pretty well. But if...
12
by: Julian | last post by:
Hi, I am having problems with a function that I have been using in my program to read sentences from a 'command file' and parse them into commands. the surprising thing is that the program works...
8
by: ashwini1680 | last post by:
I have a problem working with files in c++.This is my code for modifying a record in a file.When i execute it ,it doesn't check rec in file & comes out .What's wrong??? Many time working with...
2
by: geseeker | last post by:
Hi, i want to move the file pointer to the last 128th byte, so i can read the ID3V1 tag in an mp3 file. Here's my code to test the result of moving the file pointer: #include <iostream> #include...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.