Hello ,...
how to know if file exists or not.
i use to use stat() but i can't find out what exit code or how to know if file dosnot exists.
i thout that stat() will return some error but i can't find any :
i use to use this :
but it won't give me any error if a directory dosen't existsCode:int exit_code_of_state = -9999; exit_code_of_state == stat (filename, &stat_p);
and i know that if stat had error code of -1 he can't state it but if the file dosent exists i don't know how to know that
also how do i use the '~' sign (the user home dir) since i dosent work with (~/.myprogram) directly from c (like i did here)
Code:#include <sys/stat.h> /* declare the 'stat' structure */ #include <sys/types.h> #include <unistd.h> #include <stdio.h> /* printf */ #include <time.h> #define FILENAME "~/.myprog/" /* PUT YOUR FILE NAME HERE */ /************************************************************************/ char * format_time(time_t cal_time); /************************************************************************/ int main() { file_stat(FILENAME); return 0; } /************************************************************************/ void file_stat(char * filename) { struct stat stat_p; /* 'stat_p' is a pointer to a structure * of type 'stat'. */ /* Get stats for file and place them in * the structure. */ int exit_code_of_state = -9999;//to see that the exit code can be changed exit_code_of_state ==(int) stat (filename, &stat_p); if ( exit_code_of_state == -1) { printf(" Error occoured attempting to stat %s\n", filename); exit(0); } /* Print a few structure members. */ if (stat_p.st_size == 0 ) { //the dir dosen't exists printf("The dir dosn't exists\n creating new one "); mkdir(FILENAME , S_IRWXU); } else { return; } }



LinkBack URL
About LinkBacks


