Thread: Help with file checking

  1. #1
    C Newbie
    Join Date
    Aug 2010
    Posts
    29

    Help with file checking

    Is there any way to check if a file exists. I usually use a Mac, but for specific reasons I have to use Linux.

  2. #2

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What's wrong with stat? (Which, after all, is how you'd it on a Mac anyway, right?)

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    dont think there's a function to check if a file exists. You could check if the directory exists but not a file. There are two way to achive this. You use either call stat() to find the file information or the function will returns 0, if the file doesn't exits. Or use fopen to check if the file exsists. Like

    Code:
    int CheckFileExists( char *filename )
    {
        FILE *fp;
        if( ( fp = fopen(filename,"r") ) != NULL )
        {  
            fclose( fp );
            return 1;
        }
        return 0;
    }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    C Newbie
    Join Date
    Aug 2010
    Posts
    29

    Working on it

    Okay I am starting to check out stat. Fopen was causing a Segmentation fault on Ubuntu, so thats why I wanted help.

    Edit: I can use some help with stat. I'm not that good with structs yet.
    Last edited by Alegomaster; 01-21-2011 at 06:42 PM.

  6. #6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM