c - 如何从系统调用中提取错误



例如,在手册页中它说:

The mmap() function shall fail if:
EACCES
The fildes argument is not open for read, regardless of the protection specified,        
fildes is not open for write and PROT_WRITE was specified for a MAP_SHARED type 
mapping. 

还有很多其他情况。我假设有一种方法可以检查发生了哪个错误,但是一个小时的搜索没有任何结果。您如何检查是否发生了该特定错误?

该错误值将保存在全局变量errno中。 您可以从perror(3)获取人类可读的字符串。

如果您使用的是C++,您也可以这样做

std::cerr << strerror(errno) << std::endl;

更详细的信息可以在这里找到:

C++ perror() 的替代方法

最新更新