system() and _flushall()
- From: "Claudio Valderrama C." <user@xxxxxxxxxxx>
- Date: Thu, 1 Sep 2005 01:36:17 -0400
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
.
- Follow-Ups:
- Re: system() and _flushall()
- From: Pavel A.
- Re: system() and _flushall()
- Prev by Date: Re: Load standard DLL by #Import
- Next by Date: LPOLESTR to std::string conversion
- Previous by thread: Excel automation using VC++
- Next by thread: Re: system() and _flushall()
- Index(es):
Relevant Pages
|