checking existance

This is a discussion on checking existance within the C++ Programming forums, part of the General Programming Boards category; How do you check the existance of a file, and if theres more then one(most likely is ) what is ...

  1. #1
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63

    checking existance

    How do you check the existance of a file, and if theres more then one(most likely is ) what is the best one. Im trying to experiment more with files so I can use it to help with settings and stuff, but I dont want it to waste time rewriting the file that is already there. I know I could always just tell it not to, but I'd rather the program check if it already exists so I dont even have to run the writing code if its already there.

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    27
    Code:
    #include <stdio.h>
    int main () {
      FILE* fp;
      if ((fp = fopen("filename", "r")) != NULL) {
        // File exists if this far is reached
        fclose(fp);
      }
      return 0;
    }
    My guess is you ment more than that, but im not sure X_x

  3. #3
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    What I meant was is there a way to check if a file exists with a single function. I was thinking of something like this:
    Code:
    while (![function to check]) {
      wrtfile(); //my write func
    }
    thats basically it, but i need to find a function like that, and if I cant, then im gonna have to use your idea as its own function.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *rant about people not doing any thinking for themselves*
    Code:
    int exists( const char *filename )
    {
      FILE* fp;
      if ((fp = fopen( filename, "r")) != NULL) {
        // File exists if this far is reached
        fclose(fp);
        return 1;
      }
      return 0;
    }
    There, now we took Zarkhalar's nice little code and wrapped it up in a shiny package for you, since you can't do anything yourself.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,651
    The fastest way will be OS specific. For Windows:
    Code:
    #include <windows.h>
    
    // returns true if filename specifies an existing file
    bool file_exists(const char *filename)
    {
        DWORD attributes = GetFileAttributesA(filename);
        if ((attributes != 0xFFFFFFFF) && 
            !(attributes & FILE_ATTRIBUTE_DIRECTORY))
        {
            return true;
        }//if
    
        return false;
    }//file_exists
    For *nix, use access().

    gg

  6. #6
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Quote Originally Posted by quzah
    *rant about people not doing any thinking for themselves*
    Code:
    int exists( const char *filename )
    {
      FILE* fp;
      if ((fp = fopen( filename, "r")) != NULL) {
        // File exists if this far is reached
        fclose(fp);
        return 1;
      }
      return 0;
    }
    There, now we took Zarkhalar's nice little code and wrapped it up in a shiny package for you, since you can't do anything yourself.

    Quzah.
    Isnt that exactly what I said I was going to do if there was none? Thanks anyways, saves me the trouble of taking 5 seconds to write it

    Quote Originally Posted by LloydUzari
    thats basically it, but i need to find a function like that, and if I cant, then im gonna have to use your idea as its own function.
    Oh and code, I dun think I understand that much yet...(just started 2 weeks ago... )

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    If you don't know how to make a function I would recommend waiting to do File I/O for a while.

    A long while.

  8. #8
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    I know how, I was just asking for a way to check existance...

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    One way to check for file existence is _access in <io.h>:

    if (_access("filename.txt", 0))
    ...

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by LloydUzari
    What I meant was is there a way to check if a file exists with a single function. I was thinking of something like this:
    Code:
    while (![function to check]) {
      wrtfile(); //my write func
    }
    thats basically it, but i need to find a function like that, and if I cant, then im gonna have to use your idea as its own function.
    this is how I do it:

    Code:
    ifstream infile("filename.dat");
    if(infile)
         //file exists/is open
    infile.close();
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    code janitor
    Join Date
    Dec 2004
    Location
    California
    Posts
    1

    don't forget security

    If you create a file_exists() function based on open/close success, it may not work correctly if the file exests, but you dont have read access rights.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> One way to check for file existence is _access in <io.h>:

    the only problem with that function is it doesn't differentiate between files and directories. I've found that this is the most portable (but not ANSI) way to check for file existance:

    Code:
    #include <sys/stat.h>
    bool exists(const char * filename) {
     struct stat info;
     return stat(filename, &info) == 0 && !S_ISDIR(info.st_mode);
     }



    ITSA
    Socket Library!

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,651
    While we're keep'n this thread alive...

    >> For *nix, use access().
    This is more appropriate for determining if you're in the situation that SleepyJoe describes.

    And welcome to the boards SleepyJoe! Have look at some of the more recent threads Value-added input is always welcome.

    gg

  14. #14
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Code:
    #include <fstream>
    
    bool file_exists(const char *path) {
      std::ifstream fin(path);
      return fin.is_open();
    }
    and if theres more then one
    If there's more than one instance of a file in a given directory, you've got a problem.

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> This is more appropriate for determining if you're in the situation that SleepyJoe describes.

    no they're equally appropriate - except mine had a bug in it (the very thing it safeguarded against) and yours just takes up less space.



    ITSA
    Socket Library!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  3. Checking File Existance
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2002, 01:02 AM
  4. file existance checking /w code
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 02-08-2002, 01:40 PM
  5. checking for a files existance
    By Eber Kain in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2001, 02:50 PM

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