delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/11/22:49:20

From: makulik AT tf16 DOT mch DOT sni DOT de (Makulik.Guenther T 27528 R 91-811 WS tx24 )
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Newbie question: File I/O operations
Date: 8 Aug 1997 14:11:33 GMT
Organization: Siemens Nixdorf Inc.
Lines: 26
Message-ID: <5sf9el$7o9$1@horus.mch.sni.de>
References: <33E892B8 DOT 5F44 AT singnet DOT com DOT sg>
NNTP-Posting-Host: tmntn.mch.sni.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

teckheng AT singnet DOT com DOT sg wrote:
: I have a question about file I/O in C++.

: 	I had used the following code to open a file:
: 	
: 	fstream finout;
: 	finout.open(file, ios::out|ios::in|ios::binary);

: 	While it is able to read/write an existing file, I was not able to get
: it create a new file if "file" does not exist.  I remember that's what
: "r+" was able to do in C.
I think that the problem lies in the ios::in mode operator you have specified.
Normally the mode settings should default to create a non existing file (wich can
be explicitely switched off by specifying ios::nocreate). But it seems that there's
other behaviour if ios::in is specified, to read from a file presupposes that its 
existing. 
Try to create the file first, or use separate instances for reading and writing 
the file (I think they can be synchronized with tie()).
 
: 	At the moment, I have to test for the existence of the file, use
: finout.open(file, ios::out|ios::binary); to create the file, close and
As you see this statement (without ios::in) creates the file.

: 	Thanks in advance.
HTH
guenther

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019