checking if a file/stream exists

Discussion in 'C++' started by Leslaw Bieniasz, Jan 3, 2005.

  1. Cracow, 3.01.2005

    Hello,

    When opening a file stream in the append mode,
    either a new file is created (if a specified file does not exist),
    or an existing file is opened for adding stuff.
    Is there any way to determine, after calling the open(filename,ios::app)
    method, which of these two alternatives has occurred?
    I would like to differentiate the program operation, depending
    on whether I write to a new or existing file.

    L.Bieniasz


    *-------------------------------------------------------------------*
    | Dr. Leslaw Bieniasz, |
    | Institute of Physical Chemistry of the Polish Academy of Sciences,|
    | Department of Electrochemical Oxidation of Gaseous Fuels, |
    | ul. Zagrody 13, 30-318 Cracow, Poland. |
    | tel./fax: +48 (12) 266-03-41 |
    | E-mail: |
    *-------------------------------------------------------------------*
    | Interested in Computational Electrochemistry? |
    | Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
    *-------------------------------------------------------------------*
     
    Leslaw Bieniasz, Jan 3, 2005
    #1
    1. Advertisements

  2. Your system may contain a system specific function which allows yo
    to check for file existence.

    Another way:
    Try to open the file in non-append mode. If this works, then the
    file does exist, if not ... Close the file and open it in append
    mode. You then can now use the information you found out in
    the first open attempt to guide your operations.
     
    Karl Heinz Buchegger, Jan 3, 2005
    #2
    1. Advertisements

  3. Leslaw Bieniasz

    Shan Guest

    You can use the ifstream::tellg() (part of STL) to find the size of the
    file. If it is greater than zero then the file must have existed
    before you opened it. If the file size is zero then you can assume that
    the file is new..Hope it helps.

    -Shan
     
    Shan, Jan 3, 2005
    #3
  4. Not really.
    tellg() returns some number which can only be used for a seekg().
    No one gives a guarantee of what this number represents. It can
    be the number of bytes read so far, it need not be that way.
     
    Karl Heinz Buchegger, Jan 3, 2005
    #4
  5. Leslaw Bieniasz

    Shan Guest

    Not really.
    Correct. I missed that. I wanted to give a portable solution, which i
    think is not possible. I think stat() function can be used , even
    though it is a C function.
     
    Shan, Jan 3, 2005
    #5
  6. Leslaw Bieniasz

    Mike Wahler Guest

    Also a file with size zero might already exist.

    -Mike
     
    Mike Wahler, Jan 3, 2005
    #6
  7. Leslaw Bieniasz

    Mike Wahler Guest

    'stat()' is an extension provided by some implementations,
    where it is applicable. It's not a standard C or C++ function.

    -Mike
     
    Mike Wahler, Jan 3, 2005
    #7
  8. Leslaw Bieniasz

    Stephen Howe Guest

    When opening a file stream in the append mode,
    There is a way with standard C++ but it is cumbersome considering what you
    want to do.
    Open the file for input. If the open succeeds, it must exist.

    Stephen Howe
     
    Stephen Howe, Jan 3, 2005
    #8
  9. Hello,

    That's what I have been doing, but I feel this is not elegant.
    Aren't there a better way?

    L.B.

    *-------------------------------------------------------------------*
    | Dr. Leslaw Bieniasz, |
    | Institute of Physical Chemistry of the Polish Academy of Sciences,|
    | Department of Electrochemical Oxidation of Gaseous Fuels, |
    | ul. Zagrody 13, 30-318 Cracow, Poland. |
    | tel./fax: +48 (12) 266-03-41 |
    | E-mail: |
    *-------------------------------------------------------------------*
    | Interested in Computational Electrochemistry? |
    | Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
    *-------------------------------------------------------------------*
     
    Leslaw Bieniasz, Jan 4, 2005
    #9
  10. Not that I know of.
    Elegance is in the eye of the beholder.
    Come on, we are talking about 3 or 4 lines of code. You can
    wrap them up in a function of your own.
     
    Karl Heinz Buchegger, Jan 4, 2005
    #10
  11. Leslaw Bieniasz

    msalters Guest

    Which is a good approach, but not perfect. The file system is
    shared and not locked, so another (instance of the same) application
    can create the file, after you concluded it does not exist.
    Regards,
    Michiel Salters
     
    msalters, Jan 4, 2005
    #11
  12. Leslaw Bieniasz

    Mike Wahler Guest

    But if it fails, that does not necessarily mean it does not
    exist (e.g. could be locked by another user, insufficient
    permissions, broken net connection, etc.). There is no
    way in standard C++ to determine conclusively the existence
    or nonexistence of a file.

    -Mike
     
    Mike Wahler, Jan 5, 2005
    #12
  13. Leslaw Bieniasz

    ToeNi Guest

    Bro,
    This's what I got http://www.cplusplus.com/ref/iostream/ifstream/

    at that site may solve the problem(existence of a file).

    This is my logic:1.open the file anymode(ios::app, ios::in, ios::eek:ut)
    2.checking is_open
    example:
    bool exist;
    exist=(fileobject.is_open())?true:false;
    3.if file is not open, which mean file is not exist, then open the file
    with append mode (this is file creation).
    4.After that close the file, reopen it (any mode) (depend on you).
    5.if file is open, continue the others
     
    ToeNi, Jan 6, 2005
    #13
  14. Leslaw Bieniasz

    Mike Wahler Guest

    It might or might not mean that. Opening a file can
    fail for a variety of reasons.

    -Mike
     
    Mike Wahler, Jan 6, 2005
    #14
  15. Leslaw Bieniasz

    ToeNi Guest

    It might or might not mean that. Opening a file can
    I agree. There are two file types in C++ (text format, binary format)
    We can't open a file without knowing the file type. Append mode
    opening is work like this "first open the file , second check the
    existence , if exist, peek pointer is go to the end of the file, if
    doesn't exist , create the file & peek pointer is ready to put data".
    This is what I have learn.
    Plz correct me, if I'm wroung.
     
    ToeNi, Jan 6, 2005
    #15
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.