| | |
errno - perror vs fprintf + strerror
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Hi!
From the manpages I know, that perror evaluates the errno. So
using perror and fprintf + strerror, as coded in my example below, should result in the same error messages, but they do not.
The execution of the program leads to the following result:
bash> ./errno_test blub
fopen() failed: No such file or directory
fopen() failed
fopen() failed: Error code 29
fopen() failed: Error code Illegal seek
Can someone explain, where this behavior comes from? I think
fopen calls open, and open might invoke something such as a seek function. Is the errno set inappropriately? Or am I wrong about the
assumption, that perror and fprintf + strerror should cause the same output?
Thanks and Regards!
From the manpages I know, that perror evaluates the errno. So
using perror and fprintf + strerror, as coded in my example below, should result in the same error messages, but they do not.
The execution of the program leads to the following result:
bash> ./errno_test blub
fopen() failed: No such file or directory
fopen() failed
fopen() failed: Error code 29
fopen() failed: Error code Illegal seek
Can someone explain, where this behavior comes from? I think
fopen calls open, and open might invoke something such as a seek function. Is the errno set inappropriately? Or am I wrong about the
assumption, that perror and fprintf + strerror should cause the same output?
Thanks and Regards!
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> int main (int argc, char** argv) { if (argc != 2) { fprintf(stderr, "usage: ./error_test <file> \n"); exit(1); } /* open any non-existing file to provoke an error message */ if (NULL == fopen(argv[1], "r")) { perror ("fopen() failed"); fprintf(stderr, "fopen() failed\n"); fprintf(stderr, "fopen() failed: Error code %d\n", errno); fprintf(stderr, "fopen() failed: Error code %s\n", strerror(errno)); } return 0; }
Similar Threads
- Using sprintf (C)
- Buffers Possibly? (C)
- Pipe() and Execve() Problem (C)
- socket program (C)
- Problem with OS exception, only errno (Python)
- Need help with signals problem please... (C)
- what is the simplest way to output my results (C)
- error C2375: 'my_strdup' : redefinition; different linkage (C++)
- A project making use of FILE and file functions fopen,fgets and fclose (C)
- Why perror? (C)
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi



