delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/09/19/14:13:14

From: kuehl AT uzwil DOT informatik DOT uni-konstanz DOT de (Dietmar Kuehl)
Newsgroups: comp.lang.c,comp.lang.c++,comp.os.msdos.djgpp
Subject: Re: Can not reset an open ifstream file.
Followup-To: comp.lang.c,comp.lang.c++,comp.os.msdos.djgpp
Date: 19 Sep 1996 15:30:05 GMT
Organization: Fakultät für Mathematik und Informatik
Lines: 68
Distribution: world
Message-ID: <51rott$n1f@news.BelWue.DE>
References: <51qo44$e2 AT lion DOT cs DOT latrobe DOT edu DOT au>
Reply-To: dietmar DOT kuehl AT uni-konstanz DOT de
NNTP-Posting-Host: uzwil.informatik.uni-konstanz.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi,
Cs3prj Group 04 (cs3prj04 AT lion DOT cs DOT latrobe DOT edu DOT au) 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.

After seeing this statement and the code below, I would be perfectly
justified in saying that the fools who are using the iostream/fstream
classes should read the documention! Sorry, couldn't resist... And,
BTW, those who designed the IOStream library came up with a very good
design! It has some problems on the side of error handling but it is
easy to use and easy to extend. You just need to know what you are
doing.

: 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.

Have you ever heard of 'ios::fail()'? Well, if not, you should look up
when this flag becomes 'true' and what the consequences of this flag
being 'true' are. If you have done this, you would recognize, that this
flag has to be cleared (e.g. using 'ios::clear()') before the stream
can be used for anything useful again.

: Can any body show me that I am wrong.
: #include <fstream.h>
: #include <iostream.h>
: #include "string.hpp"

: void main()

Just a minor note: 'void main()' is non-portable (although I know that
several C++ tutorials explicitly used, * SIGH *). There are only two
portable declarations for 'main()' and these are 'int main()' and 'int
main(int ac, char *av[])' (of course, in the latter version you can
freely choose the names of the arguments). Also note, that you cannot
omit the 'int' before 'main()' because the "implicit int rule"
(formerly inherited from C) has been dropped.

: {
:   //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());

If the path of execution reaches this point, 'In.fail()' almost
certainly yields 'true' (this somewhat depends on the implementation of
'StringC's extractor). Thus, you should add the following statement:

    In.clear(ios::goodbit);

I haven't tested it but I think this should solve the problem.

:   In.seekg(0);
:   In>>Line;
:   cout<<Line<<endl;
:   In.close();
: }

--
<mailto:dietmar DOT kuehl AT uni-konstanz DOT de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019