ch2-open
Chapter_2 assert | chown |
open.c ALP, p. 40
#include <stdio.h> // fprintf(), stderr, perror()
#include <string.h> // for strerror()
#include <stdlib.h> // for exit()
#include <errno.h> // for errno macro
// errno.h includes x86_64-linux-gnu/bits/errno,h, which includes
// linux/errno.h, which includes asm-generic/errno.h, which includes
// asm-generic/errno-base.h, which defines ENOENT:
// #define ENOENT 2 /* No such file or directory */
#include <fcntl.h> // for open(), O_RDONLY
// fcntl.h includes x86_64-linux-gnu/bits/fcntl-linux.h,
// which defines O_RDONLY
int main()
{
int fd; // file descriptor
fd = open ("inputfile.txt", O_RDONLY); // open for reading only
if (fd == -1)
{ /* The open failed. Print an error message and exit: */
fprintf (stderr, "Error number: %d\n", ENOENT);
fprintf (stderr, "Error number: %d\n", errno);
fprintf (stderr, "Error opening file: %s\n", strerror(errno));
perror("main");
exit (1); // end program, signalling error
}
return 0;
}
/*
gcc open.c -o open
./open
Error number: 2
Error number: 2
Error opening file: No such file or directory
main: No such file or directory
*/
Chapter_2 assert | BACK_TO_TOP | chown |
Comments
Post a Comment