Red Hat Bugzilla – Bug 59933
C++ regression failure with fstream
Last modified: 2008-05-01 11:38:01 EDT
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.8) Gecko/20020205
Description of problem:
The constructor std::fstream(const char *filename, std::ios::in | std::ios::out)
does not create the file if it doesn't exist (the resulting open() does not have
O_CREAT mode set).
std::fstream(const char *filename, ios::out) does set O_CREAT, but then the file
is opened in write-only mode. There does not appear to be any way to create a
file with fstream in read/write mode???
Version-Release number of selected component (if applicable):
Steps to Reproduce:
1. Compile the attached file.
2. strace it
Actual Results: strace output:
open("testfile", O_RDWR) = -1 ENOENT (No such file or directory)
Expected Results: strace output:
open("testfile", O_RDWR|O_CREAT|O_LARGEFILE, 0664) = 3
(this is under gcc 2.96)
Changing the open mode in the constructor to std::ios::out results in the
open("testfile", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
So the file is now created, but it's open in write-only mode only. There does
not appear to be any way in gcc 3.1 to create a file in read/write mode.
Doesn't seem right.
Created attachment 45781 [details]
test program for this bug.
You might want to take a look at:
126.96.36.199 - Member functions [lib.filebuf.members]
filebuf::open(const char* s,ios_base::openmode mode );
There's a table that explains the open modes. In particular:
in out trunc equiv stdio
+ + - "r+"
+ + + "w+"
So, what you need to do to get the behavior you'd like with current,
standards-conforming semantics is pretty simple:
fstream f("testfile", ios::in | ios::out | ios::trunc);