delorie.com/archives/browse.cgi | search |
Newsgroups: | comp.lang.c,comp.lang.c++,comp.os.msdos.djgpp |
From: | Dan Milligan <dwm AT cci DOT com> |
Subject: | Re: Can not reset an open ifstream file. |
To: | Cs3prj Group 04 <cs3prj04 AT lion DOT cs DOT latrobe DOT edu DOT au> |
Message-ID: | <3246DDEC.41C67EA6@cci.com> |
Sender: | root AT sunsrvr6 DOT cci DOT com (Operator) |
Organization: | Northern Telecom |
References: | <51qo44$e2 AT lion DOT cs DOT latrobe DOT edu DOT au> |
Mime-Version: | 1.0 |
Date: | Mon, 23 Sep 1996 18:58:52 GMT |
Lines: | 46 |
To: | djgpp AT delorie DOT com |
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
Cs3prj Group 04 wrote: > > After running the below program I would be perfectly justified in saying > that the fools who designed the iostream/fstream classes have provided no > means of reseting an open file so that it can be read again. > > With the do while loop commented out seekg works as you would expect it to > and you see, on the screen, the first line of the file printed out twice. > > However with the do while loop uncommented the call to seekg fails under all > circumstances and you see the first line of the file printed only once. > > Can any body show me that I am wrong. > #include <fstream.h> > #include <iostream.h> > #include "string.hpp" > > void main() > { > //fstream In("main.cpp",ios::in|ios::out); > ifstream In("main.cpp"); > StringC Line; > In>>Line; > cout<<Line<<endl; > do > { > In>>Line; > } > while (!In.eof()); > In.seekg(0); > In>>Line; > cout<<Line<<endl; > In.close(); > } I had a similiar problem. THe solution is to see if an error occurred using the fail() command suxh as In.fail() and if an error occurred then clearing it by using the clear() command such as In.clear(). When you read past the eof the error occurs and you have to reset the failed flag prior to making your next read. The reason as far as I can figure is that the fail bit is a single bit and even though your have rewound the input pointer the bit remains set since the iostream does not recall the reason it was set. -- Dan Milligan
webmaster | delorie software privacy |
Copyright © 2019 by DJ Delorie | Updated Jul 2019 |