Here is the right way to use strerror_r in cout, ON GNU
----------------
strerror_r(3) - Linux man page
Name
strerror, strerror_r - return string describing error number
Synopsis
#include <string.h>
char *strerror(int errnum);
int strerror_r(int errnum, char *buf, size_t buflen);
/* XSI-compliant */
char *strerror_r(int errnum, char *buf, size_t buflen);
/* GNU-specific */
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
The XSI-compliant version of strerror_r() is provided if:
(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
Otherwise, the GNU-specific version is provided.
-----------------------------------
ON POXIS compiler(RHEL-6)
------------------
Example Program:
------------------
cat testStrerr_r.cpp
#include <string.h>
#include <iostream>
int main() {
char buf [30];
//strerror_r (0, buf, sizeof (buf));
std :: cout << "Ravi::"<<strerror_r (0, buf, sizeof (buf))<<std :: endl;
std :: cout << "Ravi::"<<strerror_r (1, buf, sizeof (buf))<<std :: endl;
std :: cout << "Ravi::"<<strerror_r (2, buf, sizeof (buf))<<std :: endl;
std :: cout << "Ravi::"<<strerror_r (11, buf, sizeof (buf))<<std :: endl;
std :: cout << "Ravi::"<<strerror_r (12, buf, sizeof (buf))<<std :: endl;
std :: cout << "Ravi::"<<strerror_r (13, buf, sizeof (buf))<<std :: endl;
std :: cout << "Ravi::"<<strerror_r (14, buf, sizeof (buf))<<std :: endl;
return 0;
}
./testStrerr_r
Ravi::Success
Ravi::Operation not permitted
Ravi::No such file or directory
Ravi::Resource temporarily unavailable
Ravi::Cannot allocate memory
Ravi::Permission denied
Ravi::Bad address
我只能確認這一點。 ''strerror_r'在ubuntu中定義爲'char * strerror_r(int,char *,size_t)',使用gcc 4.6.3。 'std :: cout << strerror_r(0,buf,sizeof(buf));'輸出「成功」。 – harpun 2013-05-05 12:38:47
那真是愚蠢。爲什麼地獄不GNU版本有不同的名稱,如果它有不同的語義?荒謬。但是,謝謝。 – spraff 2013-05-05 12:40:16
@spraff:我猜GNU'strerror_r'早於Posix之一。 – 2013-05-05 14:07:54