system() and _flushall()



Hello, people, I have a problem between system() and _flushall(). The MSVC6
documentation and the online documentation at MSDN say:

You must explicitly flush (using fflush or _flushall) or close any stream
before calling system.

Documentation on _flushall says
All buffers associated with open input streams are cleared of their current
contents.
....
If a read follows a call to _flushall, new data is read from the input files
into the buffers.

Ok, I'm doing that, but I discovered a nasty effect: I have one file open
for reading and other for writing. Not a problem if the output file is
flushed. However, on the next read from the input file using fgets, I get
EOF. I discovered (by going into the CRT) that all the small file is
already loaded on the FILE's buffer, but after flushall a new physical read
attempt is done, that obviously generates EOF. The input stream has
forgotten that there's still data in the buffer (the buffer is not destroyed
by flushall) that -under normal circumstances- would be retrieved by the
next call to fgets() or another related routine. In case my description is
not understandable, a silly example. File f has
line1
line2

I use fopen, then fgets, so I get "line1". Then another fgets will get
"line2". However
fopen, fgets reads "line1", _fflushall() to call system() since this is what
MS demands but now,
fgets returns EOF, not "line2", despite the stream's internal buffer still
has the required information.

How to satisfy MS requirements? Should I keep the position of any input file
before calling flushall, then call system and finally reset the input
streams' positions? I can't imagine a more cumbersome way. A compromise
might be using fflush(NULL) that will flush only output streams, but it's
not what MS demands and may not work in all cases in my program.

Ideas? Thanks.

C.
--
Claudio Valderrama C.
SW developer, consultant.
http://www.cvalde.net - http://www.firebirdsql.org


.



Relevant Pages

  • Re: system() and _flushall()
    ... > You must explicitly flush (using fflush or _flushall) or close any stream ... However, on the next read from the input file using fgets, I get ... > forgotten that there's still data in the buffer (the buffer is not destroyed ...
    (microsoft.public.vc.language)
  • Re: fgets() and embedded null characters
    ... > Every so often one of my fgets() based programs encounters ... > an input file containing embedded nulls. ... > the embedded nulls once they are in the buffer. ...
    (comp.lang.c)
  • fgets() and embedded null characters
    ... Every so often one of my fgets() based programs encounters ... an input file containing embedded nulls. ... read these but the embedded nulls subsequently cause problems ... the embedded nulls once they are in the buffer. ...
    (comp.lang.c)
  • Re: *scanf in Harbison and Steele
    ... The difference between fgets and gets is at least that fgets takes ... the output buffer. ... Though I've been told that stdin come with the food in C, must you declare: ...
    (comp.lang.c)
  • Re: Replacing fgets
    ... OK, for the sake of discussion, replacing fgets() with some type ... of equivalent function where you START with a unopened file really ... reset the buffer position. ... if you only want to set buffer position, ...
    (comp.lang.c)