Thread: Checking for file existence

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    Checking for file existence

    Can anyone suggest how I can check for a files existence prior to using it...

    I have found a code called F_OK - but am not sure of the syntax to use it.

    many thanks
    Sue

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    #include <stdio.h>
    
    int file_check(char *FileToCheck, char *ManipulateFile)
    {
            FILE * Existance;
            if ( ( Existance = fopen(FileToCheck, "r+") ) == 0)
    	{
    	        /* file was not found */
    	}
    	else
    	{
    		fclose(Existance); /* Make sure to close it */
    		system(ManipulateFile);
            }
    return 0;
    }
    
    int main()
    {
            file_check("c:\\path\\to\\file\\foo.txt", "c:\\windows\\notepad.exe c:\\path\\to\\file\\foo.txt");
            return 0;
    }
    Does this help?
    Last edited by Shadow; 05-05-2002 at 11:15 PM.
    The world is waiting. I must leave you now.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Except "r+" will create the file if it doesn't exist
    Perhaps "r"

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I think the F_OK you found is from the access (or _access) function. I don't know which OS/compiler you are using and if your compiler supports the function but here's an example:
    Code:
    #include  <io.h>
    #include  <stdio.h>
    #include  <stdlib.h>
    
    int main()
    {
      // 0 = check for existence, 2 = check for write permission
      // 4 = check for read permission, 6 = check for read/write permission
      if(_access( "C:\\file.xt", 0 ) != -1)
      {
        printf("File exists\n");
    
        if(_access( "C:\\file.txt", 2 ) != -1)
          printf("File has write permission\n");
      }
    }
    This (above) is a windows example. If you are using unix/linux and the access function is supported you probably need to include unistd.h and you can use R_OK, W_OK, X_OK and F_OK.
    Code:
    #include  <unistd.h>
    #include  <stdio.h>
    #include  <stdlib.h>
    
    int main()
    {
      if(access("file.xt", F_OK) != -1)
      {
        printf("File exists\n");
    
        if(access("file.txt", R_OK|W_OK) != -1)
          printf("File has read/write permission\n");
      }
    }
    Regards,
    Monster

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM