Re: iostreams equivalent to C's fopen "r+"
From: Rick Noelle (anonymous_at_jdictionary.com)
Date: 11/18/03
- Next message: lilburne: "Re: private destructor"
- Previous message: Arve Sollie: "private destructor"
- In reply to: Dave O'Hearn: "Re: iostreams equivalent to C's fopen "r+""
- Next in thread: Mike Wahler: "Re: iostreams equivalent to C's fopen "r+""
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 18 Nov 2003 22:27:28 GMT
On 18 Nov 2003, Dave O'Hearn wrote:
> "Chuck McDevitt" <Chuck_McDevitt@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 have two systems, a Red Hat 6.2 with gcc 2.95.3, and a Red Hat 9
> with gcc 3.2.2. The ancient system has an ios::nocreate, and it's
> definately creating the files unless I put ios::nocreate in there. I
> don't know what the newer system does by default, but it has no
> ios::nocreate, so I can't put that in my code without conditional
> compilation, or it won't compile on Red Hat 9 at all.
>
> I think I have to use the hack I found online, where you open for 'in'
> mode first, then if it succeeds, reopen for in|out mode. It's odd that
> 'nocreate' isn't standard, since it's easy enough for a library to
> fake even if the OS doesn't support it... and if the library doesn't
> fake it, all the user code has to fake it instead. But that didn't
> happen, so I will just have to fake it myself, oh well. Thanks.
Hi Dave,
This does the trick for me on RedHat 9.0 with gcc 3.2.2. If the file does
not exist, it will not be created. The excellent book "C++ Primer Plus
4th Edition" by Stephen Prata has a table that lists the C modes like r+
with their equivalent C++ modes and the equivalent to r+ was identified as
ios_base::in | ios_base::out. Give it a shot, it works for me. If the
file does not exist, nothing happens. If it exists, it is written to.
Regards,
Rick
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("test.txt",ios_base::in | ios_base::out );
fout << "This is a test." << endl;
fout.close();
}
--------------------------------------------
My real email address excluded to avoid SPAM
- Next message: lilburne: "Re: private destructor"
- Previous message: Arve Sollie: "private destructor"
- In reply to: Dave O'Hearn: "Re: iostreams equivalent to C's fopen "r+""
- Next in thread: Mike Wahler: "Re: iostreams equivalent to C's fopen "r+""
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|