473,406 Members | 2,273 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,406 software developers and data experts.

fstream problem

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, even after the seekg is
executed. I've also tried closing the fstream and reopening it. Nothing is
working. Anyone have any ideas?

How do you read a file twice in VC++?
May 12 '06 #1
7 1963
Never mind, I think I figured it out. Thanks.

"Michael C" wrote:
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, even after the seekg is
executed. I've also tried closing the fstream and reopening it. Nothing is
working. Anyone have any ideas?

How do you read a file twice in VC++?

May 12 '06 #2
Michael C wrote:
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, even after the seekg is
executed. I've also tried closing the fstream and reopening it. Nothing is
working. Anyone have any ideas?

How do you read a file twice in VC++?


Michael:

You probably need to call clear().

David Wilkinson
May 12 '06 #3
Thanks David. You're right, that's what I did to fix it in fact, just took
me a while to figure it out. I'm surprised that if you seekg to the
beginning of the file you have to clear() to reset the eof and other flags.
Strange.

Thanks again.

"David Wilkinson" wrote:
Michael C wrote:
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, even after the seekg is
executed. I've also tried closing the fstream and reopening it. Nothing is
working. Anyone have any ideas?

How do you read a file twice in VC++?


Michael:

You probably need to call clear().

David Wilkinson

May 12 '06 #4

"Michael C" <Mi******@discussions.microsoft.com> a écrit dans le message de
news: 78**********************************@microsoft.com...
Thanks David. You're right, that's what I did to fix it in fact, just
took
me a while to figure it out. I'm surprised that if you seekg to the
beginning of the file you have to clear() to reset the eof and other
flags.
Strange.


That's the way standard I/O streams work : Once a stream is in error state
(and having the stream go past the end of the file put the stream into error
state, so that you can iterate through the file with a "while (mystream)"
loop), you cannot do anything on the stream : You must call clear before.

Arnaud
MVP - VC
May 13 '06 #5

"Arnaud Debaene" <ad******@club-internet.fr> wrote in message
news:ex**************@TK2MSFTNGP03.phx.gbl...

"Michael C" <Mi******@discussions.microsoft.com> a écrit dans le message
de news: 78**********************************@microsoft.com...
Thanks David. You're right, that's what I did to fix it in fact, just
took
me a while to figure it out. I'm surprised that if you seekg to the
beginning of the file you have to clear() to reset the eof and other
flags.
Strange.


That's the way standard I/O streams work : Once a stream is in error state
(and having the stream go past the end of the file put the stream into
error state, so that you can iterate through the file with a "while
(mystream)" loop), you cannot do anything on the stream : You must call
clear before.

Arnaud
MVP - VC


I haven't tested it yet, but after googling "seekg" I found plenty of
examples that seekg() to the end of file, then seekg() to other positions in
a file, without a clear() operation. That's what led me to believe it
wasn't necessary at first.

Thanks
May 13 '06 #6

"Mike C#" <xx*@yyy.com> a écrit dans le message de news:
UK*************@fe09.lga...

"Arnaud Debaene" <ad******@club-internet.fr> wrote in message
news:ex**************@TK2MSFTNGP03.phx.gbl...

"Michael C" <Mi******@discussions.microsoft.com> a écrit dans le message
de news: 78**********************************@microsoft.com...
Thanks David. You're right, that's what I did to fix it in fact, just
took
me a while to figure it out. I'm surprised that if you seekg to the
beginning of the file you have to clear() to reset the eof and other
flags.
Strange.


That's the way standard I/O streams work : Once a stream is in error
state (and having the stream go past the end of the file put the stream
into error state, so that you can iterate through the file with a "while
(mystream)" loop), you cannot do anything on the stream : You must call
clear before.

Arnaud
MVP - VC


I haven't tested it yet, but after googling "seekg" I found plenty of
examples that seekg() to the end of file, then seekg() to other positions
in a file, without a clear() operation. That's what led me to believe it
wasn't necessary at first.


Yeah, but by reading the file till the end, you've (probably) got the stream
go one *after* the end of the file, this is what put the stream in error.

Arnaud
MVP - VC
May 13 '06 #7
"Arnaud Debaene" <ad******@club-internet.fr> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I haven't tested it yet, but after googling "seekg" I found plenty of
examples that seekg() to the end of file, then seekg() to other positions
in a file, without a clear() operation. That's what led me to believe it
wasn't necessary at first.


Yeah, but by reading the file till the end, you've (probably) got the
stream go one *after* the end of the file, this is what put the stream in
error.

Arnaud
MVP - VC


Ah, that makes sense. I'm using the getline() method to read a line at a
time. I didn't realize getline() read one past the eof - just assumed it
stopped once it encountered an eof.

Thanks again
May 13 '06 #8

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

Similar topics

3
by: Frédéric Manzanares | last post by:
hello, my problem: I want to habe one Class with write and read in a file. i have overloaded the operator >> and <<. class c_File { public : fstream fs;
6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
3
by: David Blasdell | last post by:
This appears very strange to me, I'm having a problem with a fstream object inside a class which is not being called directly from main (another class calls the class that contains the fstream...
3
by: ratzeel | last post by:
The following snippet code throws an error while compiling on SUN os.. Any idea how to resolve this... #include <iostream.h> #include <fstream.h> #include <math.h> #include <algorithm>...
2
by: NewToCPP | last post by:
I am having some trouble including "iostream" "fstream" to my code. I tried the following ways and base times it did not work. What is the problem? test.cc: ======= ..... #include...
5
by: ehui928 | last post by:
The following program is used to open a file in mode both read and append, but it has a problem that the file can't be opened. #include <iostream> #include <fstream> #include <cstdlib> ...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
2
by: jjcp | last post by:
I would like to say thanks in advance for insight anyone can shed on this for me. Long story short from time to time I need to us C++ to take a list of file and make an index out of them in...
3
by: shyam | last post by:
Hi All I want to know if there is any problem with using fstream.getLine(char*, int ) function. My problem is that when I read a file using it, the program aborts when it reads the...
3
by: Ioannis Vranos | last post by:
Hi, I am experimenting with fstream type, I have written the following code: #include <iostream> #include <fstream> #include <string> int main() { using namespace std;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.