Topic: fstream
Author: "Matthew Darwin" <mattsjunkemail@yahoo.com>
Date: Fri, 13 Apr 2001 02:13:50 GMT Raw View
I was wondering what the standard had to say about the following questions
(and if there are differences in compiler implementations):
If I open a file to read with an ifstream object, is that stream loaded ONCE
at the time of opening with the entire contents of the file? Or does the
stream access the file as needed to get data? As a programmer, this is
transparent. But constant disk access could severly affect performance.
In the reverse case: When I want to save information to a file and insert
it to an ofstream, does this automatically write to the disk immediately?
If not, how often is writing done. Can you force or delay writing?
Thanks for your help,
Matthew.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Andrea Ferro" <AndreaF@UrkaDVD.it>
Date: Fri, 13 Apr 2001 10:43:46 GMT Raw View
"Matthew Darwin" <mattsjunkemail@yahoo.com> wrote in message
news:fh5B6.1070$8R6.520726@news.uswest.net...
> I was wondering what the standard had to say about the following questions
> (and if there are differences in compiler implementations):
>
> If I open a file to read with an ifstream object, is that stream loaded ONCE
> at the time of opening with the entire contents of the file? Or does the
> stream access the file as needed to get data? As a programmer, this is
> transparent. But constant disk access could severly affect performance.
>
> In the reverse case: When I want to save information to a file and insert
> it to an ofstream, does this automatically write to the disk immediately?
> If not, how often is writing done. Can you force or delay writing?
There's no guarantee by the standard that file I/O is either actually buffered
or unbuffered. However all the implementations are actually buffered (beside the
OS itself do some buffering if it lets you read and write at byte level).
If all you want to control is that the output data is actually out, you can use
flush. If you need more control on buffering you must provide your own streambuf
implementation (actually it's template basic_streambuf). If you just want to
check what your library does, check the library implementation of streambuf for
files (it is template basic_filebuf). But I warn you: understanding internals of
those structures is pretty tought.
--
Andrea Ferro
---------
Brainbench C++ Master. Scored higher than 97% of previous takers
Scores: Overall 4.46, Conceptual 5.0, Problem-Solving 5.0
More info http://www.brainbench.com/transcript.jsp?pid=2522556
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Martin Sebor <sebor@roguewave.com>
Date: Fri, 13 Apr 2001 21:01:38 GMT Raw View
Matthew Darwin wrote:
>
> I was wondering what the standard had to say about the following questions
> (and if there are differences in compiler implementations):
>
> If I open a file to read with an ifstream object, is that stream loaded ONCE
> at the time of opening with the entire contents of the file? Or does the
> stream access the file as needed to get data? As a programmer, this is
> transparent. But constant disk access could severly affect performance.
This behavior is actually controlled by the stream buffer (basic_filebuf)
associated with the file stream object. The buffer allocates an internal
character buffer into which it reads and converts the external characters.
The initial size and the location of the internal buffer are implementation
specific but can be changed by calling the virtual member function setbuf().
>
> In the reverse case: When I want to save information to a file and insert
> it to an ofstream, does this automatically write to the disk immediately?
> If not, how often is writing done. Can you force or delay writing?
The buffer can either be forcefully flushed, usually by calling the
ostream::flush() member function or it is flushed automatically whenever
it fills up. Depending on how the underlying I/O is implemented, there may
be additional delays caused by the C library I/O and its own buffering,
but a quality iostream implementation will write through the C I/O and
directly to the OS (or perhaps even use the POSIX file API directly).
Regards
Martin
>
> Thanks for your help,
>
> Matthew.
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]