how to check if a file exists?

This is a discussion on how to check if a file exists? within the C++ Programming forums, part of the General Programming Boards category; how do you check if a file exists before you attempt to open it or write to it?...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    106

    how to check if a file exists?

    how do you check if a file exists before you attempt to open it or write to it?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Usually you try to open it and if it fails to open then you handle that error.

    You can attempt to open it for reading. If that succeeds then the file exists. If the file doesn't exist then it will fail to open for reading.

    There could always be some other error causing an existing file not to open, but that will be fairly rare. Depending on what platform you're on there are other more specific ways to tell that might be more accurate.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Isn't there a way to browse the existing folder? This should be portable through unix solaris linux windows and all the major ones.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    There is no standard C++ solution. I would try boost's filesystem library. There may also be Posix functions that will work across the platforms you need.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    thanks, i'll have to get my co-worker to make the funciton give a return value on his dataset loading function then.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by elninio View Post
    Isn't there a way to browse the existing folder? This should be portable through unix solaris linux windows and all the major ones.
    If it's only those OS's that you're concerned with, then they all should support the stat() function which gets info about a file. Other more exotic OS's may not support stat().
    But it would still be a good idea to abstract out the function you're trying to do and call whatever OS specific function is available on your platform.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    thank you

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    what about this : http://www.cplusplus.com/reference/i...ebuf/open.html
    What are its limitations to this for checking if a file exists in a folder or not?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    This is the same as my original suggestion (although I'm not sure why you need to use the filebuf, just use the ifstream). If the file fails to open for reading, you won't be able to tell if it doesn't exist or if there was some other error. Otherwise it's fine.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by Daved View Post
    This is the same as my original suggestion (although I'm not sure why you need to use the filebuf, just use the ifstream). If the file fails to open for reading, you won't be able to tell if it doesn't exist or if there was some other error. Otherwise it's fine.
    This simply returns a pointer to the first bit, so it is fully accurate (isn't it?).

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    What do you mean "first bit"?

    If the file does not exist, the function returns null. If the file exists but cannot be opened for reading (perhaps because of certain permissions), the function returns null. How can you tell the difference?

    As I said, you might not care about the difference, in which case simply using the ifstream will be fine, but it isn't 100% accurate.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by Daved View Post
    What do you mean "first bit"?

    If the file does not exist, the function returns null. If the file exists but cannot be opened for reading (perhaps because of certain permissions), the function returns null. How can you tell the difference?

    As I said, you might not care about the difference, in which case simply using the ifstream will be fine, but it isn't 100% accurate.
    Tell me more please. Or you could recommend me to a resource that will be fine.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Daved is asking you what you mean by "exists". Do you mean, that you can access it? Then just trying to open it will answer your question. Do you mean, whether the file is in the filesystem somewhere, even if it is locked or protected? You will have to use nonstandard system calls to answer that.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    I don't know any good resources off the top of my head. What are you confused about?

    It's pretty simple. If you try to open a file for reading and it fails, most of the time that's because it doesn't exist. Occasionally it is because of some other error (like a lack of read permissions).

    If you try to open the file with an fstream (or ifstream or filebuf) then the only information you get is that the file failed to open. You don't know if it is because it does not exist or if it is because the user does not have permission to read that file.

    So the question is, for your program, is it good enough to just know that it failed without knowing why? If you are writing files in the same directory in your application, then it will be very rare that you won't be allowed to read one. So maybe it's ok.

    If it's not ok, then use a third party library like boost filesystem instead of using the fstream open trick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. How to check if a file exists
    By ElWhapo in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2004, 04:16 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. check if the file exists
    By ipe in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 08:18 AM

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