By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,106 Members | 2,679 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,106 IT Pros & Developers. It's quick & easy.

reading file created with tmpfile?

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Oliver Knoll" <tk****@bluewin.ch> wrote in message
news:b2**************************@posting.google.c om...
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
rh*******@pobox.com
Nov 13 '05 #2

P: n/a
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
Nov 13 '05 #3

P: n/a
In <b2**************************@posting.google.com > tk****@bluewin.ch (Oliver Knoll) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4

P: n/a
In article <3f****************@news.nl.net>
Richard Bos <rl*@hoekstra-uitgeverij.nl> writes, in part:
... 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...)
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.