errno value จะไม่มีเลข 0 เนื่องจากเลข 0 หมายถึงไม่มีข้อผิดพลาดในโปรแกรม
// err_p1_3.c#include<stdio.h>#include<errno.h>intmain() { // If a file is opened which does not exist, // then it will be an error and corresponding // errno value will be set FILE * fp; // opening a file which does // not exist. fp =fopen("text.txt","r"); printf(" Value of errno: %d\n ", errno); return0; }
// err_p1_5.c// C program to illustrate the // use of perror() #include<errno.h>#include<stdio.h>#include<stdlib.h>// Driver Code intmain() { FILE* fp; // Now try to open same file fp =fopen("file.txt","r"); if (fp ==NULL) { perror("Error: "); return (-1); } // Close the file pointer fclose(fp); return (0); }
// err_p1_6.cpp// C++ program to illustrate the // use of perror() #include<iostream>#include<errno.h>// Driver Code intmain() { FILE* fp; fp =fopen("file.txt","r"); if (fp ==NULL) { perror("Error: "); return (-1); } // Close the file pointer fclose(fp); return (0); }
// err_p1_7.c// C program to illustrate the // use of strerror() #include<errno.h>#include<stdio.h>#include<string.h>// Driver Code intmain() { FILE* fp; fp =fopen("file.txt","r"); if (fp ==NULL) { printf("Value of errno: %d\n ", errno); printf("The error message is : %s\n",strerror(errno)); return (-1); } // Close the file pointer fclose(fp); return (0); }
// err_p1_8.cpp// C++ program to illustrate the // use of strerror() #include<iostream>#include<cerrno>#include<string.h>usingnamespace std; // Driver Code intmain() { FILE* fp; // Now try to open same file fp =fopen("file.txt","r"); if (fp ==NULL) { cout <<"Value of errno: "<< errno << endl; cout <<"The error message is : "<<strerror(errno) << endl;return (-1); } // Close the file pointer fclose(fp); return (0); }
Value of errno: 2
The error message is : No such file or directory
Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University