Topic: File Manipulation
Author: Steve Clamage <stephen.clamage@eng.sun.com>
Date: 1997/05/08 Raw View
Sean Rohead wrote:
>
> Is there a standard way to open a file for reading and writing if the file
> does not currently exist?
>
> fstream::open(filename,ios::in | ios::out) will fail if the file isn't
> there already. Isn't there some way to force the file to be created? A
> quick glance at the standard didn't yield any insights.
Original iostreams did not specify exactly how opening for read/write
should work. Consequently, current implementations vary. The draft
standard now defines all operations in terms of stdio operations.
You have only three options in opening a stdio file for read/write:
"r+": file must already exist
"w+": file will be truncated to zero length if it exists
"a+": all writes will be forced to the end of the file
Append "b" to the string for binary mode.
The corresponding fstream flags are:
in|out: same as "r+"
in|out|trunc: same as "w+"
Add the "binary" flag for binary mode.
I think the draft should also specify in|out|app to correspond to
"a+" but it currently does not allow that combination. I believe
that combination was accidently left out, and I will investigate.
Perhaps you want in|out|trunc correspoding to "w+". You can read
and write the file at arbitrary points, and the file need not already
exist. It will be truncated to zero length if it does exist.
If you want to open a file for arbitrary read/write, preserving the
existing contents, creating the file if it does not already exist, you
cannot do it in one operation. You could do something like this:
fstream f("filename", ios::in|ios::out|ios::binary);
if( ! f.is_open() )
f.open("filename", ios::in|ios::out|ios::binary|ios::trunc);
In other words, if the "r+" open fails, open it as "w+", which will
create an empty file.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Sean Rohead" <sean@erudite.com>
Date: 1997/05/06 Raw View
Is there a standard way to open a file for reading and writing if the file
does not currently exist?
fstream::open(filename,ios::in | ios::out) will fail if the file isn't
there already. Isn't there some way to force the file to be created? A
quick glance at the standard didn't yield any insights. If I delete the
ios::in, it works, but then I can't read from it! This is what I've come
up with so far, but it is UGLY and I must be overlooking something. BTW, I
am using MS Visual C++ 5.0
pagefile.open(filename.c_str(),std::ios::in | std::ios::out |
std::ios::binary);
// BEGIN_KLUDGE
if(!pagefile)
{
/* If file didn't exist, opening for reading will fail. */
pagefile.clear();
// create dummy file
pagefile.open(filename.c_str(),std::ios::out | std::ios::binary);
pagefile.put(0);
pagefile.close();
// Now that the file exists, open it for reading and writing.
pagefile.open(filename.c_str(),std::ios::in | std::ios::out |
std::ios::binary);
}
// END_KLUDGE
Any suggestions? This seems like a pretty big gap in the standard if what
I'm suggesting isn't possible.
Sean Rohead
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]