File Open Error C
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack fopen error handling c Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack c fopen file path Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error handling in fopen error codes file opening up vote 2 down vote favorite [Question 1] When I open a file into a function, generally I do something like this: int read_file (char *filename) { FILE *fin; if ( !(fin = fopen(filename, "r")) ) return 1; /* ... */
C Errno Example
return fclose(fin); } int main () { char filename[100]; if ( read_file(filename) ) { perror(filename); exit(1); } return 0; } Generally 0 return value is for errors (right?) then I can change the previous code into: int read_file (char *filename) { FILE *fin; if ( !(fin = fopen(filename, "r")) ) return 0; /* ... */ return !fclose(fin); } int main () { char filename[100]; if ( !read_file(filename) ) { perror(filename); exit(1); } return 0; } But I think that the first code is more clean. Another option c perror is only change return 1; into return -1; (in the first code that I wrote). What's the best version? [Question 2] If I must handle more errors, is it correct a code like this? int read_file (char *filename, int **vet) { FILE *fin; if ( !(fin = fopen(filename, "r")) ) { perror(filename); return 1; } * vet = malloc (10 * sizeof(int)); if ( *vet == NULL ) { perror("Memory allocation error.\n"); return 1; } /* ... */ return fclose(fin); } int main () { char filename[100]; int *vet; if ( read_file(filename, &vet) ) exit(1); return 0; } c file error-handling fopen fclose share|improve this question asked Jan 21 '14 at 19:52 ᴜsᴇʀ 506314 will not (fin = fopen(filename, "r") be always true? –Dipto Jan 21 '14 at 19:59 2 In general we use 0 as the default error return as 0 refers to false and non null values refers to true. But when different errors needs to be handled we use negative values for errors. In your second question it's better to return different values for different errors to be able to know exactly which error caused the fail. –rullof Jan 21 '14 at 20:02 It's more customary in C to use a negative value as error indicator and zero or a positive number for success. In your function, zero or a positive return value could indicate the number of vecs that were read, where zero means, the file is okay but didn't have any vets in it. I like the
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this c open site About Us Learn more about Stack Overflow the company Business Learn more
Strerror
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Read File C
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Unable to http://stackoverflow.com/questions/21267716/error-handling-in-file-opening open a file with fopen() up vote 6 down vote favorite 2 I've been trying to open a file and output text, but I keep getting errors. So I thought I would start at the very beginning and just try opening the file. This is my code: #include
there are ways to do error handling. Of course the programmer needs to prevent errors during coding and should always test the return values of functions called https://www.codingunit.com/c-tutorial-error-handling-exception-handling by the program. A lot of C function calls return a -1 or http://www.gnu.org/s/libc/manual/html_node/Opening-and-Closing-Files.html NULL in case of an error, so quick test on these return values are easily done with for instance an ‘if statement’. In previous tutorials we already mention that this behavior (returning numbers to indicate an error) is also used in Unix or Linux like operating systems. For instance if a program open error successful ends the return value of the program is zero. If the program ends with an error usually a number larger than zero is returned (for example 1). (With command ‘echo $?’ on the command line you can display the return code of a program that has previously run). So the one thing you need to remember is that you (the programmer) are responsible for error open error c handling. You’re the person that needs to make sure that a program will gracefully terminate and not just CRASH unexpectedly! It is you that need to take appropriate action depending on the return values of function calls. Global Variable errno The global variable errno is used by C functions and this integer is set if there is an error during the function call. To make use of errno you need to include errno.h and you need to call ‘extern int errno;’ Let us take a look at an example: #include
file fcntl.h, while close is declared in unistd.h. Function: int open (const char *filename, int flags[, mode_t mode]) Preliminary: | MT-Safe | AS-Safe | AC-Safe fd | See POSIX Safety Concepts. The open function creates and returns a new file descriptor for the file named by filename. Initially, the file position indicator for the file is at the beginning of the file. The argument mode (see Permission Bits) is used only when a file is created, but it doesn’t hurt to supply the argument in any case. The flags argument controls how the file is to be opened. This is a bit mask; you create the value by the bitwise OR of the appropriate parameters (using the ‘|’ operator in C). See File Status Flags, for the parameters available. The normal return value from open is a non-negative integer file descriptor. In the case of an error, a value of -1 is returned instead. In addition to the usual file name errors (see File Name Errors), the following errno error conditions are defined for this function: EACCES The file exists but is not readable/writable as requested by the flags argument, the file does not exist and the directory is unwritable so it cannot be created. EEXIST Both O_CREAT and O_EXCL are set, and the named file already exists. EINTR The open operation was interrupted by a signal. See Interrupted Primitives. EISDIR The flags argument specified write access, and the file is a directory. EMFILE The process has too many files open. The maximum number of file descriptors is controlled by the RLIMIT_NOFILE resource limit; see Limits on Resources. ENFILE The entire system, or perhaps the file system which contains the directory, cannot support any additional open files at the moment. (This problem cannot happen on GNU/Hurd systems.) ENOENT The named file does not exist, and O_CREAT is not specified. ENOSPC The directory or file system that would contain the new file cannot be extended, because there is no disk space left. ENXIO O_NONBLOCK and O_WRONLY are both set in the flags argument, the file named by filename is a FIFO (see Pipes and FIFOs), and no process has the file open for reading. EROFS The file resides on a read-only file system and any of O_WRONLY, O_RDWR, and O_TRUNC are set in the flags argument, or O_CREAT is set and the file does not alr