tk****@bluewin.ch (Oliver Knoll) wrote:
According to my ANSI book, tmpfile() creates a file with wb+ mode
(that is just writing, right?). How would one reopen it for reading?
It _is_ opened for reading. That's what the + is for. wb+ means:
truncate file to zero length, then open it for update, a.k.a for reading
_and_ writing. It's equivalent to w+b. There's also r+b, which opens for
updating without truncating; and there's a+b, which opens for updating,
but starts writing at end-of-file. And obviously there are the
equivalent text modes, without the b.
rewind() is somewhat of a red herring; you need either rewind() or a
positioning function if you write, and then want to change to reading,
or vice versa (or only when going from writing to reading, fflush()).
This is true for all update modes. However, it's not the rewind() which
allows you to read /per se/; if you open a file r+b, you can choose to
either read or write initially. You _could_ choose to read from the file
as the first operation in w+b mode, but it isn't very useful to read an
empty file <g>.
So no, rewind() doesn't change the mode. the mode is, and remains,
binary update. But you do need to reposition the file position between
reading and writing, using rewind(), fseek(), fsetpos(), or fflush().
There is one exception: if an input from an update stream reaches end of
file, you may immediately follow it with a write operation.
Richard