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

This is a discussion on what is the error code of stat for non existent file ? within the C Programming forums, part of the General Programming Boards category; Hello ,... how to know if file exists or not. i use to use stat() but i can't find out ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    112

    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 :

    Code:
    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)



    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;
      }
      
    }
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

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

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

    ERRORS
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21