[cs13001] Input and Output on the same file

Mikhail Nesterenko mikhail at cs.kent.edu
Thu Feb 27 22:12:14 EST 2014


> 
> Today I asked whether a program could input-from and output-to the same
> file simultaneously.  You asked me to email the question to you so that you
> could give a  more complete answer.
> 
> My curiosity in this case is not about how such an odd behavior could be
> used in a program, but is instead about what is going on "behind the
> scenes."  A followup question is: can more than one input stream be
> associated with the same file simultaneously, and the same case for two
> output streams with one file?

Okay, it turns out that ofstream and ifstream may be used to open the
same file. Moreover, there is another stream type called fstream that
can be used to read and write the same file simultaneously.

Internally, the streams in the program and further in the operating
system provide buffering to speed up input/output operations. Disk
operations are an order of magnitude slower than RAM
access. Moreover, part of the time is spent on moving of the
read/write head. The OS attempts to accelerate reading and writing by
doing multiple operating at once, optimizing read/write head movement,
or, if there is a request for data that is buffered in memory,
eliminating disk access altogether. A lot more on this is coming in
the Operating Systems course.

Thanks,
--
Mikhail


More information about the cs13001 mailing list