Examples of errorno in C++
The following examples illustrate how we can use errno in different scenarios.
Example 1
The below example demonstrates the usage of errno for reading a file because when we try to open a file it may throw an error.
C++
// C++ program to demonstrate the use of errno for reading a // file #include <cerrno> #include <cstring> #include <fstream> #include <iostream> using namespace std; int main() { // Defining the filename to be opened. const char * filename = "myfile.txt" ; // Attempting to open the file using ifstream. ifstream file(filename); // Checking if the file was not opened successfully or // not. if (!file) { // Switch case based on the error number (errno). switch ( errno ) { case ENOENT: // Case for file not existing. cerr << "Error: File '" << filename << "' doesn't exist." << endl; break ; case EACCES: // Case for access denied. cerr << "Error: Permission denied for file '" << filename << "'." << endl; break ; default : // Default case for other errors. cerr << "Error opening file '" << filename << "': " << strerror ( errno ) << endl; break ; } // Return with error code 1 to indicate failure. return 1; } // Closing the file if it was opened successfully. file.close(); // Return 0 to indicate successful completion. return 0; } |
Output
Error: File 'myfile.txt' doesn't exist.
Explanation: The above example shows how to handle error that we get when we try to open a file using ifstream
. It checks for conditions that may give error like file not existing or access being denied by using the errno
variable.
Note: Always clear errno after handling errors and use errno=0 to reset the value after handling an error.
Example 2
The below example demonstrates the use of errno to detect error in mathematical operations.
C++
// C++ program to demonstrate the use of errno to detect // error in mathematical operations. #include <cerrno> #include <cmath> #include <iostream> using namespace std; int main() { // Variable initialization with a value that will cause // a domain error in sqrt double x = -1.0; // Attempt to calculate the square root of x double result = sqrt (x); // Check if errno is set to EDOM, indicating a domain // error in the sqrt function if ( errno == EDOM) { // Print an error message to stderr and return with // error code 1 cerr << "Error: Cannot take the square root " "of a negative number." << endl; return 1; } // If no error, print the result cout << "Square root of " << x << " is: " << result << endl; // Return 0 indicating success return 0; } |
Output
Error: Cannot take the square root of a negative number.
Explanation: The above example checks for EDOM after taking the mathematical values Error. When we try to calculate the square root of a negative number like (-2
), that are actually not defined and leads to a domain error.
How to use errno in C++?
In C++ errno
is a preprocessor macro that is used for error indication and reporting errors that occur during a function call. It contains error codes so if a call to a function fails somehow then the errno is set to a value that corresponds to the error.
errno is defined in the header file <cerrno>
i
n C++.
Contact Us