473,387 Members | 1,721 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,387 software developers and data experts.

problems with seekg

Hi I'm trying to read a textfile and print it's content twice to the std::cout but it won't work the second time. I think it has something to do with the seekg(0) command but I don't know.

here is my code:
the seekg.cpp file:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main(){
  5.     ifstream in("data.txt",ios::in);
  6.     if(!in){
  7.         cerr<<"file could not be opened"<<endl;
  8.     }
  9.     char line[200];
  10.     while(in.getline(line,200)){
  11.         cout<<line<<endl;
  12.     }
  13.     in.seekg(0);
  14.     while(in.getline(line,200)){
  15.         cout<<line<<endl;
  16.     }    
  17. }
  18.  
the data.txt file:
Expand|Select|Wrap|Line Numbers
  1. michael
  2. santiago
  3. carlos
  4. seppi
  5.  
my output is:
$./a
michael
santiago
carlos
seppi
$
Oct 15 '06 #1
3 3685
tyreld
144 100+
The problem is that the eof bit gets set after the first loop, and any file operations on the stream will now fail. You have to clear the bit by calling the aptly named clear member.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main(){
  5.         ifstream in("data.txt",ios::in);
  6.         if(!in){
  7.                 cerr<<"file could not be opened"<<endl;
  8.         }
  9.         char line[200];
  10.         while(in.getline(line,200)){
  11.                 cout<<line<<endl;
  12.         }
  13.  
  14.         // Clear the eofbit so we can perform more actions on the stream.
  15.         in.clear();
  16.  
  17.         in.seekg(0, ios::beg);
  18.         while(in.getline(line,200)){
  19.                 cout<<line<<endl;
  20.         }
  21. }
  22.  
Oct 16 '06 #2
Thank you very much!

One more question. Is there some kind of refference where all thees tricks with files are explained?
Oct 16 '06 #3
tyreld
144 100+
The Bjarne Stroustrup book "The C++ Programming Language (Special Edition)" is probably the best reference for ISO standard C++. However, it isn't exactly the easist book to read.

The following is also a good online quick reference to the iostream library:

C++ Library Reference
Oct 16 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kapil Khosla | last post by:
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 #include "stdafx.h" using std::cerr; using std::endl;...
1
by: Darren | last post by:
hi, i'm trying to write some code to generate seperate files with the name of a call number, and containing the call data, from one big file containing all data. but when i run it, i get the...
1
by: wtnt | last post by:
Hello. I've searched all over and haven't seen another thread with this problem. Please bear with me as I try to explain. thanks. :) I have some programs that need to be cross-platform...
2
by: Brian Ronk | last post by:
I'm having a strange error. I have a Debian stable server and I'm using gcc 3.3, but it looks like I have 3.4 installed as well. I recently made a change to a program that takes data from two...
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...
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()...
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...
1
by: Rubenintx | last post by:
Need some help with this code / Using UNIX. The goals is to read a big file (2,000,000) lines, using fork() to create children to help in the process. I am able to get the program (C++)...
1
by: fmufti | last post by:
I would like to check if the f_count is a valid position for seekg in the file or not. What I mean is if I pass a position to seekg and that does not exist in the file then e.g if...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.