Cool_Coder
Cool_Coder

Reputation: 5083

How do I use errno in C++

I cannot understand what the errno library in c++ is for? What types of errors are set in it and how do I know which number stands for which error?

Does it affect program execution?

Upvotes: 19

Views: 40217

Answers (3)

JJ on SE
JJ on SE

Reputation: 417

No offense to the author of the top-voted answer, but I feel like it is misleading and inaccurate. errno.h is not exactly "part of C", it is defined by Posix (https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/) and it is highly likely that your c++ standard library implementation relies upon the Posix api on your system (which is defined, yes, in C). In fact if you take a look here for documentation on the standard library include cerrno (https://en.cppreference.com/w/cpp/error/errno) you'll see:

Several standard library functions indicate errors by writing positive integers to errno.

So to answer your question of "what is it for", errno basically looks like a "global variable" and works like that for your purposes and you absolutely need it to find out what posix calls failed as the pattern is generally that the calls return <0 and you get the error code by dereferencing errno.

#include <iostream>
#include <fstream>
#include <cerrno>

int main(int argc, char* argv[]) 
{
    std::ifstream f("/file_that_does_not_exist.txt");
    if (!f) {
        std::cout << "file open failed: " << errno << "\n";
    }
    return 0;
}

Upvotes: 0

Eboubaker
Eboubaker

Reputation: 800

This is how you print the errno in modern c/c++

#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *f = fopen("./scressenshot.png", "r");
    if(f == NULL) {
        printf("fopen failed(): %s\n", strerror(errno))
    }
    return 0;
}

Upvotes: -2

Nikko
Nikko

Reputation: 4252

errno.h is part of the C subset of C++. It is used by the C library and contains error codes. If a call to a function fails, the variable "errno" is set correspondingly to the error.

It will be of no use if you're using the C++ standard library.

In C you have functions that translate errno codes to C-strings. If your code is single threaded, you can use strerror, otherwise use strerror_r (see http://www.club.cc.cmu.edu/~cmccabe/blog_strerror.html)

For instance in C it works like this:

 int result = call_To_C_Library_Function_That_Fails();

 if( result != 0 )
 {
    char buffer[ 256 ];
    strerror_r( errno, buffer, 256 ); // get string message from errno, XSI-compliant version
    printf("Error %s", buffer);
     // or
    char * errorMsg = strerror_r( errno, buffer, 256 ); // GNU-specific version, Linux default
    printf("Error %s", errorMsg); //return value has to be used since buffer might not be modified
    // ...
 }

You may need it of course in C++ when you're using the C library or your OS library that is in C. For instance, if you're using the sys/socket.h API in Unix systems.

With C++, if you're making a wrapper around a C API call, you can use your own C++ exceptions that will use errno.h to get the corresponding message from your C API call error codes.

Upvotes: 21

Related Questions