473,700 Members | 2,806 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
10 6723
"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 found out a little more about this. So far as I can tell, you are
right about it being implementation defined to create the file or not.

But it seems that all (or most) classic iostreams implementations
create the file and have some form of ios::nocreate to suppress it,
while all standard iostreams implementations will not create the file
when you open for in|out mode.

This is somewhat more satisfying for me to rely on... "All new
compilers do it one way," was sketchy, but "Really every standard
iostreams implementation does it this way, and really every outdated
classic iostreams implementation does it the other way," makes me feel
comfortable relying on the behavior of the file not being created, and
just saying my application requires standard, not classic, iostreams.

Still I can't find any text in the actual Standard that promises it,
unfortunately.

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

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

Similar topics

9
7419
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
2404
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 between #include <iostream> // (or any include file) which is used in this
1
2116
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
6289
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
4190
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
3370
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
3604
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 job. I created the stringstream in main() and pass it as pointer into a few threads. Those threads...
3
19787
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
1634
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
9206
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
8963
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
8916
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
7802
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...
1
6558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4400
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...
0
4652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3083
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
2022
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.