Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > null FILE* pointers

Reply
Thread Tools

null FILE* pointers

 
 
kelvSYC
Guest
Posts: n/a
 
      06-23-2005
With std::fopen, will it return a null pointer if the file does not
exist? If so, how do I make it so that it will attempt to create a
file if it doesn't exist? Would moving to fstreams/filebufs address
these issues?

--
I am only a mirage.
 
Reply With Quote
 
 
 
 
Alipha
Guest
Posts: n/a
 
      06-23-2005
The documentation (eg,
http://www.die.net/doc/linux/man/man3/fopen.3.html ) says that if you
open the file for write or append (in addition to read if you'd like)
the file will be created if it doesn't exist.

And std::fstream would be preferred in c++ anyway. stream
insertion/extraction operators (<< and >>) are safer and more
extendable and such than printf/scanf style functions.

 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      06-23-2005


kelvSYC wrote:
> With std::fopen, will it return a null pointer if the file does not
> exist?


It depends on the mode argument to fopen().

> If so, how do I make it so that it will attempt to create a
> file if it doesn't exist?


That depends on the mode argument to fopen(). Did you read up on that?
Your documentation should explain.




Brian

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-23-2005


Default User wrote:
> kelvSYC wrote:
> > With std::fopen, will it return a null pointer if the file does not
> > exist?

>
> It depends on the mode argument to fopen().


It may also depend on platform-specific things, such as file
permissions. That is, a file may exist but the application not have
permission to open it. That's why the trick of trying to open a file
for reading is not always an adequate test for file existence.



Brian

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      06-24-2005
kelvSYC <> wrote:

(Assuming std::fopen behaves more or less like fopen() from C

> With std::fopen, will it return a null pointer if the file does not
> exist?


fopen returns a null pointer if the attempt to open the file fails.
This can happen for various reasons; one of which, of course, is that
the file does not exist. However, if the mode is "w" or "a", the file
will be created if it does not exist; if you get a null pointer in one
of these modes, something else has presumably gone wrong.

> If so, how do I make it so that it will attempt to create a
> file if it doesn't exist?


If you're opening for read, you can use "a+" as the mode; this will
open the file for read and write and create the file if it does not
exist.

> Would moving to fstreams/filebufs address
> these issues?


Probably, but I'll leave the answer to knowledgeable types.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
VB.NET Null to SQL Null (ASP.NET 2.0 GridView) Kivak Wolf ASP .Net 2 06-28-2005 02:01 PM



Advertisments