Thread: what is the error code of stat for non existent file ?

  1. #1
    Registered User
    Join Date
    Apr 2007

    what is the error code of stat for non existent file ?

    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 :

    int exit_code_of_state = -9999; 
    exit_code_of_state  ==  stat (filename, &stat_p);
    but it won't give me any error if a directory dosen't exists

    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)

    #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()
    	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);
    				/* 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);
    why Gaos didn't had a wife ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    ~ is expanded by the shell, not by low level API calls.

    Read the manual page for stat().
    Quote Originally Posted by a man page
    On success, zero is returned. On error, -1 is returned,
    and errno is set appropriately.

    EBADF filedes is bad.

    ENOENT A component of the path file_name does not exist,
    or the path is an empty string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM