Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // apple xcode
- // paulogp
- /* file_exists: checks if the file exists */
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #define MAX_VECT_LENGTH 255
- int file_exists (char *the_file);
- int file_exists (char *the_file)
- {
- FILE *the_file_ptr;
- // open file
- the_file_ptr = fopen (the_file, "r");
- // if is not there...
- if (the_file_ptr == NULL)
- return 0;
- else
- return 1;
- }
- int main (int argc, const char * argv[])
- {
- char the_file_name[MAX_VECT_LENGTH];
- char *the_file_ptr;
- char the_file_path[MAX_VECT_LENGTH];
- int the_result = 0;
- // get file path / name
- printf("ficheiro: ");
- fgets (the_file_name, sizeof(the_file_name), stdin);
- // token \n
- // NOTE: the_file_ptr points to the_file_name
- the_file_ptr = strtok (the_file_name, "\n");
- // output current directory
- getcwd(the_file_path, 255);
- printf ("path: %s\n", the_file_path);
- // file existence
- the_result = file_exists (the_file_ptr);
- if (the_result == 0)
- printf ("o ficheiro nao existe!\n");
- else
- printf ("ficheiro encontrado!\n");
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement