Checking if file exists?

This is a discussion on Checking if file exists? within the C++ Programming forums, part of the General Programming Boards category; Is there a method in C++ for checking if a certain file exists in the directory of the program? For ...

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Checking if file exists?

    Is there a method in C++ for checking if a certain file exists in the directory of the program? For example, checking if there is a file named foobar1.txt, and if there is, open a new file named foobar2.txt? Is this possible without using some external library or platform dependant code?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try to open the file for reading - if that's possible, then it exists. If so, close it and get a new name, repeat until a non-existant name.

    It is not entirely perfect, as someone else may have the filename opened in exclusive mode, so you can't open the file - but if the files you are creating aren't likely to be opened in exclusive mode, then that problem can be ignored]

    The other potential problem is the race-condition that happens if you have multiple instances, you may end up with two instances trying to creat exactly the same filename/number combination, because both found that files up to 7 exist, so 8 is the right new number.

    Both can be solved by trying again if the creation of the new file fails.

    --
    Mats

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by Neo1 View Post
    Is there a method in C++ for checking if a certain file exists in the directory of the program? For example, checking if there is a file named foobar1.txt, and if there is, open a new file named foobar2.txt? Is this possible without using some external library or platform dependant code?
    You could try opening the file in question for input and see if it fails. Not foolproof, because the file could fail to open for other reasons besides not being present. And of course, race conditions, which can't really be solved.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    But if i use std:fstream foo("bar.txt"), and the file doesn't exist, a new one is created, isn't it? How would i check if a new file is created, or if an existing one is opened?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Open for input, not output. std::ifstream is for input.

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Daved View Post
    Open for input, not output. std::ifstream is for input.
    Yes ofcourse, i knew that <.<

    Well, std::ifstream doesn't seem to create a new file when i try to open it, but i'm still not sure how my program would know whether or not ifstream was succesful in opening the file?

    Is it possible to do something like this:
    Code:
    bool file_exists;
    file_exists = std::ifstream foo("bar.txt");
    So, the bool would be false if ifstream failed in locating the file? In other words, does the constructor for ifstream return a 0 if it fails? And if not, how could i then do it?

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Here:
    Code:
    if(foo){
    //it could open file
    }

  8. #8
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by prog-bman View Post
    Here:
    Code:
    if(foo){
    //it could open file
    }
    Awesome, thanks alot guys

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    There is also the is_open() function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 02:13 AM
  5. Checking to see if a file exists...
    By Util_Mark in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2002, 04:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21