null FILE* pointers

Discussion in 'C++' started by kelvSYC, Jun 23, 2005.

  1. kelvSYC

    kelvSYC Guest

    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.
    kelvSYC, Jun 23, 2005
    #1
    1. Advertising

  2. kelvSYC

    Alipha Guest

    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.
    Alipha, Jun 23, 2005
    #2
    1. Advertising

  3. kelvSYC

    Default User Guest

    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
    Default User, Jun 23, 2005
    #3
  4. kelvSYC

    Default User Guest

    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
    Default User, Jun 23, 2005
    #4
  5. 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.
    Christopher Benson-Manica, Jun 24, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kivak Wolf
    Replies:
    2
    Views:
    10,066
    Kivak Wolf
    Jun 28, 2005
  2. vizlab
    Replies:
    3
    Views:
    4,203
    Michael Bar-Sinai
    Oct 17, 2007
  3. Replies:
    5
    Views:
    26,630
    Mike Schilling
    Mar 29, 2006
  4. jason
    Replies:
    13
    Views:
    876
    John B. Matthews
    May 14, 2010
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    674
Loading...

Share This Page