Advertisement
paulogp

file_exists

Aug 14th, 2011
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. // apple xcode
  2. // paulogp
  3.  
  4. /* file_exists: checks if the file exists */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8.  
  9. #define MAX_VECT_LENGTH 255
  10.  
  11. int file_exists (char *the_file);
  12.  
  13. int file_exists (char *the_file)
  14. {
  15.     FILE *the_file_ptr;
  16.    
  17.     // open file
  18.     the_file_ptr = fopen (the_file, "r");
  19.    
  20.     // if is not there...
  21.     if (the_file_ptr == NULL)
  22.         return 0;
  23.     else
  24.         return 1;
  25. }
  26.  
  27. int main (int argc, const char * argv[])
  28. {
  29.     char the_file_name[MAX_VECT_LENGTH];
  30.     char *the_file_ptr;
  31.     char the_file_path[MAX_VECT_LENGTH];
  32.     int the_result = 0;
  33.    
  34.     // get file path / name
  35.     printf("ficheiro: ");
  36.     fgets (the_file_name, sizeof(the_file_name), stdin);
  37.     // token \n
  38.     // NOTE: the_file_ptr points to the_file_name
  39.     the_file_ptr = strtok (the_file_name, "\n");
  40.    
  41.     // output current directory
  42.     getcwd(the_file_path, 255);
  43.     printf ("path: %s\n", the_file_path);
  44.    
  45.     // file existence
  46.     the_result = file_exists (the_file_ptr);
  47.    
  48.     if (the_result == 0)
  49.         printf ("o ficheiro nao existe!\n");
  50.     else
  51.         printf ("ficheiro encontrado!\n");
  52.    
  53.     return (0);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement