473,714 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9836
"Oliver Knoll" <tk****@bluewin .ch> wrote in message
news:b2******** *************** ***@posting.goo gle.com...
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.ne t>
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 (40°39.22'N, 111°50.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
1778
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 enctype="multipart/form-data" action="" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="3000000" /> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /> </form>
14
2677
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 afterwards, you are prompted to do a 'Save-As' for a file to download. How do they do that without the classic "Headers already sent" error?
2
3066
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 array). I cannot change anything in the program but can add some features by which I can convert this array of data into a file. The easiest thing would be to write the data into a file (in hard disk) and use it. But I will be working on thousands...
5
3290
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 FileA // open tmpFile
6
5296
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 report any error while writing to the open file handle. On Windows, file shows-up again in explorer and automatically deleted finally when program ends. On Unix, same thing happens if file is on nfs mounted drive, but in this case, actual file is...
2
1764
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 client side. It was working perfect on testing platform(Win XP IIS) and previous production server(Win 2000). Recently when I'd upgrade the server to Win 2003, my web server of it keeps
0
1019
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 following: ----------------------------Listener major coding-------------------------------------------- Line 1 Dim strAppPath As String = HttpRuntime.AppDomainAppPath
3
919
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, without having to hardcode the name of each field? Regards DW Marker wrote:
18
14632
by: Coffee Pot | last post by:
Thanks for any advice. ~ CP
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.