reading file created with tmpfile?

O

Oliver Knoll

According to my ANSI book, tmpfile() creates a file with wb+ mode
(that is just writing, right?). How would one reopen it for reading?

I got the following (which works):

FILE *tmpFile = tmpfile();

/* write into tmpFile */
...

/* is this correct? */
rewind (tmpFile);
while (fgets (line, MAXSIZE, tmpFile) {
/* put content of tmpFile into another file /*
...
}

Does rewind change the mode? Or why am I able to read from the tmpFile
this way? (I've seen in other posts that other people use rewind as
well this way).

Thanks, Oliver
 
R

Richard Bos

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
 
R

Russell Hanneken

Oliver Knoll said:
According to my ANSI book, tmpfile() creates a file with wb+ mode
(that is just writing, right?).

No, it's for reading and writing.

"rb+" = Open for reading and writing, starting at the beginning
"wb+" = Open for reading and writing (replace file if it exists)
"ab+" = Open for reading and writing (starting at the end if file exists)
How would one reopen it for reading?

You can't reopen it, because if you close a file created with tmpfile(), the
file will cease to exist. What you did (call rewind) is fine. To switch
from writing to reading, you have to either call fflush or a
file-positioning function (like rewind).

Regards,

Russell Hanneken
(e-mail address removed)
 
D

Dan Pop

In said:
According to my ANSI book, tmpfile() creates a file with wb+ mode
(that is just writing, right?).

What does your ANSI C book say about mode "wb+"? NEVER make assumptions
about the language: whenever in doubt, consult your book.
How would one reopen it for reading?

No need to reopen it.
I got the following (which works):

FILE *tmpFile = tmpfile();

/* write into tmpFile */
...

/* is this correct? */

Hard to say, without seeing the code.
rewind (tmpFile);
while (fgets (line, MAXSIZE, tmpFile) {
/* put content of tmpFile into another file /*
...
}

Note that you're using a *binary* stream. Depending on how exactly you've
written the data into it, the usage of fgets() may or may not make sense.
Does rewind change the mode?
Nope.

Or why am I able to read from the tmpFile this way?

Because it was opened in mode "wb+".

Dan
 
C

Chris Torek

... 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. ...

Note that "a" modes not only *start* writing at end-of-file, but
also force *all* writes to append. That is, given a file opened
with mode "a", "a+", "a+b", or "ab+", seeking to the beginning of
the file and then writing *still* appends to the end of the file,
rather than overwriting any existing contents.

Sometimes this turns out to be what one wants, and sometimes it
does not. In particular, if you want to open-or-create a file,
but be able to write in any position within that file, mode "a"
will not do it. Neither "w" nor "r+" will do the trick either,
because "w" will truncate the file if it exists, and "r+" will
not create it if it does not exist. The "create if needed but
do not truncate if exists" mode string is simply missing.

(It would be nice if C99 had fixed this...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top