472,961 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

reading file created with tmpfile?

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
4 9735
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jay Donnell | last post by:
My script stopped working today after working perfectly for a month. It still works perfectly on our test server just not on our live server. The form that I use is this: <form...
14
by: edykstra | last post by:
Hello, If you point your browser to this page, http://prdownloads.sourceforge.net/vnc-tight/tightvnc-1.2.9_x86_viewer.zip?use_mirror=umn the server sends you the HTML page, and very soon...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
5
by: Magix | last post by:
Hi, This is about File I/O Operation. I want to delete a record from a file, let say FileA. Any one has any good and efficient example/approach? void DeleteRecord(int LineNum) { // open...
6
by: Kiran | last post by:
Hi, I have program, which opens file at the startup and logs error messages to the file, file handle is closed at the end of the program. However if file is deleted in-between, program do not...
2
by: Rabbit | last post by:
Dear All, I've been tried various configuration and did install SP1 on Windows 2003 Server. The problem now that I have is an aspx page located on the web site for taking the file post by...
0
by: Rabbit | last post by:
Dear all, My application contains 2 parts(Server and Client), the server side is having a ASP.net page as listener for client side to post file, the main coding to receive posting file as...
3
by: ElTibetano | last post by:
Hi all, I found this ideas (below) from an old post, my question is, if I choose alternative number 3, with two selects and one for the header, is there a way to automate that code? I mean,...
18
by: Coffee Pot | last post by:
Thanks for any advice. ~ CP
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.