achille67
asked on
does the file exit?
hey ****s,
How do i check if a file exits before doing something with it ?(C language)
How do i check if a file exits before doing something with it ?(C language)
ASKER
i am partially agree on the answer: if the file does not exist, i would to create it using creat( ) but i am not sure how i should pass the pointer to it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#include <sys/stat.h>
#include <errno.h>
...
struct stat stat_p;
if (stat (filename, &stat_p) == -1)
{
if (errno == ENOENT)
{
// file does not exist (or file name is emtpy)
... // do something
}
}
Using stat() allows you to do some more testing on the file (e.g. find out if it's really a normal file and not a link or a directory, ...)
hongjun