Link to home
Start Free TrialLog in
Avatar of achille67
achille67

asked on

does the file exit?

hey ****s,
How do i check if a file exits before doing something with it ?(C language)
Avatar of hongjun
hongjun
Flag of Singapore image

You can use stat() function to check file exist


#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
Avatar of achille67
achille67

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
Avatar of manish_regmi
manish_regmi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial