473,544 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iostreams equivalent to C's fopen "r+"

I want to open a file for both reading and writing, but when I do
"ios::in|ios::o ut" with an fstream, it creates the file if it doesn't
already exist. I don't like that last part. C's fopen has the "r+"
mode, where the file will open for reading "+ also writing", but it
will not create a new file if one isn't already there; it fails with
an error. POSIX also has this behavior with it's 'open' call, you just
leave the 'create' bit out, and you can open for read/write without
creating.

But I can't figure out how to do this with iostreams. The only
ios::openmode values I know of are: app, ate, binary, in, out,
trunc... and none of them have to do with create-if-doesn't-exist. So
far as I can tell, with iostreams, that is implicit in ios::out,
whether you want it or not.

Is there a way, or do I have to open the file for reading to check
that it exists, then close it and reopen it for read/write?

Thanks in advance for any advice.

--
Dave O'Hearn
Jul 22 '05 #1
10 6701

"Dave O'Hearn" <da******@pobox .com> wrote in message
news:3e******** *************** ***@posting.goo gle.com...
I want to open a file for both reading and writing, but when I do
"ios::in|ios::o ut" with an fstream, it creates the file if it doesn't
already exist.
It shouldn't.
I don't like that last part. C's fopen has the "r+"
mode, where the file will open for reading "+ also writing", but it
will not create a new file if one isn't already there; it fails with
an error. POSIX also has this behavior with it's 'open' call, you just
leave the 'create' bit out, and you can open for read/write without
creating.

But I can't figure out how to do this with iostreams.
The following works for me (assuming that the file whose name
is specified does not previously exist):

#include <cstdlib>
#include <fstream>
#include <ios>
#include <iostream>

int main()
{
std::fstream f("C:/nonexistent.fil e", std::ios::in | std::ios::out);
if(!f)
{
std::cerr << "could not open file\n";
return EXIT_FAILURE;
}

return 0;

}

Output:

could not open file
Visual inspect confirms that the file was not created.
The only
ios::openmode values I know of are: app, ate, binary, in, out,
trunc... and none of them have to do with create-if-doesn't-exist. So
far as I can tell, with iostreams, that is implicit in ios::out,
whether you want it or not.
No.

Is there a way, or do I have to open the file for reading to check
that it exists, then close it and reopen it for read/write?


See above. What implementation are you using?

-Mike
Jul 22 '05 #2

"Dave O'Hearn" <da******@pobox .com> wrote in message
news:3e******** *************** ***@posting.goo gle.com...
I want to open a file for both reading and writing, but when I do
"ios::in|ios::o ut" with an fstream, it creates the file if it doesn't
already exist. I don't like that last part. C's fopen has the "r+"
mode, where the file will open for reading "+ also writing", but it
will not create a new file if one isn't already there; it fails with
an error. POSIX also has this behavior with it's 'open' call, you just
leave the 'create' bit out, and you can open for read/write without
creating.

But I can't figure out how to do this with iostreams. The only
ios::openmode values I know of are: app, ate, binary, in, out,
trunc... and none of them have to do with create-if-doesn't-exist. So
far as I can tell, with iostreams, that is implicit in ios::out,
whether you want it or not.

Is there a way, or do I have to open the file for reading to check
that it exists, then close it and reopen it for read/write?

Thanks in advance for any advice.

--
Dave O'Hearn


You are obviously on Windows. As far as I know, it's implementation
defined if the file gets created or not.
In the Windows implementation it does, on most Unix systems it does not.

Windows has a ios::_Nocreate you can set.
Jul 22 '05 #3

"Chuck McDevitt" <Ch************ @c-o-m-c-a-s-t.net> wrote in message
news:zPdub.2263 31$HS4.1967992@ attbi_s01...

"Dave O'Hearn" <da******@pobox .com> wrote in message
news:3e******** *************** ***@posting.goo gle.com...
I want to open a file for both reading and writing, but when I do
"ios::in|ios::o ut" with an fstream, it creates the file if it doesn't
already exist. I don't like that last part. C's fopen has the "r+"
mode, where the file will open for reading "+ also writing", but it
will not create a new file if one isn't already there; it fails with
an error. POSIX also has this behavior with it's 'open' call, you just
leave the 'create' bit out, and you can open for read/write without
creating.

But I can't figure out how to do this with iostreams. The only
ios::openmode values I know of are: app, ate, binary, in, out,
trunc... and none of them have to do with create-if-doesn't-exist. So
far as I can tell, with iostreams, that is implicit in ios::out,
whether you want it or not.

Is there a way, or do I have to open the file for reading to check
that it exists, then close it and reopen it for read/write?

Thanks in advance for any advice.

--
Dave O'Hearn


You are obviously on Windows. As far as I know, it's implementation
defined if the file gets created or not.
In the Windows implementation it does, on most Unix systems it does not.


The example I tested and posted was done with MSVC++v6.0 on Win982E.
The file was not created.

-Mike
Jul 22 '05 #4
"Chuck McDevitt" <Ch************ @c-o-m-c-a-s-t.net> wrote:
You are obviously on Windows. As far as I know, it's implementation
defined if the file gets created or not.
In the Windows implementation it does, on most Unix systems it does not.

Windows has a ios::_Nocreate you can set.


I have two systems, a Red Hat 6.2 with gcc 2.95.3, and a Red Hat 9
with gcc 3.2.2. The ancient system has an ios::nocreate, and it's
definately creating the files unless I put ios::nocreate in there. I
don't know what the newer system does by default, but it has no
ios::nocreate, so I can't put that in my code without conditional
compilation, or it won't compile on Red Hat 9 at all.

I think I have to use the hack I found online, where you open for 'in'
mode first, then if it succeeds, reopen for in|out mode. It's odd that
'nocreate' isn't standard, since it's easy enough for a library to
fake even if the OS doesn't support it... and if the library doesn't
fake it, all the user code has to fake it instead. But that didn't
happen, so I will just have to fake it myself, oh well. Thanks.

--
Dave O'Hearn
Jul 22 '05 #5
On 18 Nov 2003, Dave O'Hearn wrote:
"Chuck McDevitt" <Ch************ @c-o-m-c-a-s-t.net> wrote:
You are obviously on Windows. As far as I know, it's implementation
defined if the file gets created or not.
In the Windows implementation it does, on most Unix systems it does not.

Windows has a ios::_Nocreate you can set.


I have two systems, a Red Hat 6.2 with gcc 2.95.3, and a Red Hat 9
with gcc 3.2.2. The ancient system has an ios::nocreate, and it's
definately creating the files unless I put ios::nocreate in there. I
don't know what the newer system does by default, but it has no
ios::nocreate, so I can't put that in my code without conditional
compilation, or it won't compile on Red Hat 9 at all.

I think I have to use the hack I found online, where you open for 'in'
mode first, then if it succeeds, reopen for in|out mode. It's odd that
'nocreate' isn't standard, since it's easy enough for a library to
fake even if the OS doesn't support it... and if the library doesn't
fake it, all the user code has to fake it instead. But that didn't
happen, so I will just have to fake it myself, oh well. Thanks.


Hi Dave,

This does the trick for me on RedHat 9.0 with gcc 3.2.2. If the file does
not exist, it will not be created. The excellent book "C++ Primer Plus
4th Edition" by Stephen Prata has a table that lists the C modes like r+
with their equivalent C++ modes and the equivalent to r+ was identified as
ios_base::in | ios_base::out. Give it a shot, it works for me. If the
file does not exist, nothing happens. If it exists, it is written to.

Regards,
Rick

#include <fstream>
using namespace std;

int main()
{

ofstream fout;
fout.open("test .txt",ios_base: :in | ios_base::out );
fout << "This is a test." << endl;
fout.close();

}
--------------------------------------------
My real email address excluded to avoid SPAM

Jul 22 '05 #6
"Dave O'Hearn" <da******@pobox .com> wrote in message
news:3e******** *************** ***@posting.goo gle.com...
I think I have to use the hack I found online, where you open for 'in'
mode first, then if it succeeds, reopen for in|out mode. It's odd that
'nocreate' isn't standard, since it's easy enough for a library to
fake even if the OS doesn't support it... and if the library doesn't
fake it, all the user code has to fake it instead. But that didn't
happen, so I will just have to fake it myself, oh well. Thanks.


It appears to me that you're suffering from a broken implementation.
std::ios::in | std::ios::out should not create the file if it does
not exist. Perhaps consider an upgrade, to a newer version or a
different vendor.

-Mike
Jul 22 '05 #7
"Mike Wahler" <mk******@mkwah ler.net> wrote:
"Dave O'Hearn" <da******@pobox .com> wrote:
I think I have to use the hack I found online, where you open for 'in'
mode first, then if it succeeds, reopen for in|out mode. It's odd that
'nocreate' isn't standard, since it's easy enough for a library to
fake even if the OS doesn't support it... and if the library doesn't
fake it, all the user code has to fake it instead. But that didn't
happen, so I will just have to fake it myself, oh well. Thanks.


It appears to me that you're suffering from a broken implementation.
std::ios::in | std::ios::out should not create the file if it does
not exist. Perhaps consider an upgrade, to a newer version or a
different vendor.


If I could find a promise to that effect in the Standard, I would hack
around the old platforms and just hope that the behavior goes away
someday. But I can't find anything like it. A search on "openmode"
finds nothing but default parameters in declarations, and one very
terse table describing the 6 openmode flags, with no talk of file
creation. A search on "in|" finds 4 default paramaters in
declarations; a search on "in |" finds nothing. If it's in there
somewhere, I can't find it. It is just not enough for me that, for the
time being, one or two major platforms think the file should not be
created when this happens.

--
Dave O'Hearn
Jul 22 '05 #8
On 18 Nov 2003, Dave O'Hearn wrote:
"Mike Wahler" <mk******@mkwah ler.net> wrote:
"Dave O'Hearn" <da******@pobox .com> wrote:
I think I have to use the hack I found online, where you open for 'in'
mode first, then if it succeeds, reopen for in|out mode. It's odd that
'nocreate' isn't standard, since it's easy enough for a library to
fake even if the OS doesn't support it... and if the library doesn't
fake it, all the user code has to fake it instead. But that didn't
happen, so I will just have to fake it myself, oh well. Thanks.


It appears to me that you're suffering from a broken implementation.
std::ios::in | std::ios::out should not create the file if it does
not exist. Perhaps consider an upgrade, to a newer version or a
different vendor.


If I could find a promise to that effect in the Standard, I would hack
around the old platforms and just hope that the behavior goes away
someday. But I can't find anything like it. A search on "openmode"
finds nothing but default parameters in declarations, and one very
terse table describing the 6 openmode flags, with no talk of file
creation. A search on "in|" finds 4 default paramaters in
declarations; a search on "in |" finds nothing. If it's in there
somewhere, I can't find it. It is just not enough for me that, for the
time being, one or two major platforms think the file should not be
created when this happens.


Not sure if you saw my earlier post but I found a combination that works
like C's r+ on RedHat 9.0 with gcc 3.2.2. This does not create a file
if it does not exist which if I read correctly, is what you were looking
for. The correct combination is:

fout.open("file .txt",ios_base: :in | ios_base::out );

Also, I mentioned Prata's book and later I noticed this comment in it:

"Compatibil ity Note:
File I/O was perhaps the least standardized aspect of C++ in its
earlier days, and many older compilers don't quite conform to the
current standard. Some, for example, used modes such as nocreate
that are not part of the current standard."

At any rate, the above fout line works as if you had used nocreate in C on
my system.

Best Regards,
Rick

--------------------------------------------
My real email address excluded to avoid SPAM

Jul 22 '05 #9
Rick Noelle <an*******@jdic tionary.com> wrote:
Not sure if you saw my earlier post but I found a combination that works
like C's r+ on RedHat 9.0 with gcc 3.2.2. This does not create a file
if it does not exist which if I read correctly, is what you were looking
for. The correct combination is:

fout.open("file .txt",ios_base: :in | ios_base::out );
I don't think I need any tricks in Red Hat 9.0. Your trick seems to be
opening an ofstream in in|out mode, even though ofstreams are only
meaningful for writing? I will remember that in case I need it
someday, but in my present application, I definately need fstreams,
because I read and write to the file. Also, Red Hat 9 is not my
problem. The problem is that at least one older platform with gcc 2.x
will create zero-sized files without failing, unless a nonstandard
ios::nocreate flag is used. And unfortunately, the Standard does not
seem to say anything against this, so I am hesitant to blame the
platform for being old. I'm certainly annoyed at the platform, but I'm
not sure I have a right to blame it.
Also, I mentioned Prata's book and later I noticed this comment in it:

"Compatibil ity Note:
File I/O was perhaps the least standardized aspect of C++ in its
earlier days, and many older compilers don't quite conform to the
current standard. Some, for example, used modes such as nocreate
that are not part of the current standard."
Since posting here, I learned about this old 'nocreate' thing, and
that it isn't standard... but it also doesn't seem to be standard that
openning in|out won't create a zero-sized file instead of failing. So
even if some miracle got rid of gcc 2.x and I didn't have to support
it anymore, who knows what other platforms would still have the same
behavior, and not be in any hurry to change it.

The decision I'm trying to make now is, if even if the Standard
doesn't promise this behavior, maybe all modern platforms give it
anyway, and will keep giving it into the future. If they went that
way, I would feel comfortable hacking around the file opens on the old
platforms, and doing it the simple way on most platforms. I like the
simple way more, as it only opens the files once each, and file opens
can be expensive.
At any rate, the above fout line works as if you had used nocreate in C on
my system.


--
Dave O'Hearn
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
7405
by: dover | last post by:
For the code, outputfile << *p << endl; Someone suggests: Don't use endl here; it is flushing output stream every time. Use plain '\n'. What's the difference of using "endl" and "\n" above? What's the meaning of flushing output stream? Isn't that what we want? Thanks!
39
2362
by: TonyJeffs | last post by:
Great book - I like the way that unlike other books, AC++ explains as much as possible about every piece of code discussed, so I'm not left thinking, "well...OK... I get line 12, but I wonder what the rest of it means...". Still, I have some questions, that are frustrating me:- Grateful for any comments. 1. What is the difference...
1
2103
by: clusardi2k | last post by:
Hello, I have a shared drive on SGI, Linux, and Windows. The fact that I'm using a shared drive may be mute information. The problem is within the same program a second call to fopen does not create a file if the file has been deleted. I would like to use fopen for its pointer return value to solve this.
145
6161
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two programs: /*** a.c ***/
2
4171
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain how to use Command-Line Arguments inside C program.
7
3358
by: clusardi2k | last post by:
Hello, I have a shared drive on SGI, Linux, and Windows. A second call to fopen doesn't create the file if it has been deleted. I would like to use fopen for its pointer return value to solve this. What is the best way to fix this problem?
7
3592
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent tho network to a client so that errors and debug messages can be displayed simultaneously anywhere. I tried to use a std::stringstream to do the...
3
19773
by: Lord0 | last post by:
Hi there, First of all I apologise for being a PHP noob. Is there a PHP equivalent of Java's Jasper Reports? http://jasperforge.org/sf/projects/jasperreports Basically Jasper is a bunch of libraries which allow you to create reports, in various formats:
1
1625
by: tskrish | last post by:
Hi, I'm looking for the VB 6 equivalent "GET" in C#. I have a DAT file in binary format." I can read the file using "BinaryReader class - Read...() Method" but the problem is I need a proper chunk of data to be read. For example VB 6 uses the below code to read a DAT File, Input is a DAT File
0
7424
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...
0
7365
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...
0
7607
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. ...
1
7376
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...
0
7709
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...
0
5909
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...
0
4918
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1841
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

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.