Mail Archives: djgpp/1997/08/11/22:49:20
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 -