How do I use errorno and _get_errno?
Calling system() to run an external .exe and checking error code upon errors:
#include <errno.h>
#include <stdlib.h>
function()
{
errno_t err;
if( system(ta开发者_如何学CilCmd) == -1) //if there is an error get errno
{
//Error calling tail.exe
_get_errno( &err );
}
}
First two compile errors:
error C2065: 'err' : undeclared identifier
error C2065: 'errno_t' : undeclared identifier
Not sure why as I am including the required and optional header files?
Any help is appreciated. Thank You.A typical usage is like:
if (somecall() == -1) {
int errsv = errno;
printf("somecall() failed\n");
if (errsv == ...) { ... }
}
which is taken from here.
Just use 'errno' without any declaration. It is a macro that expands to an int value.
In the world of Standard C, the type 'errno_t
' is defined by TR24731-1 (see Do you use the TR 24731 'safe' functions? for more information) and you have to 'activate it' by defining '__STDC_WANT_LIB_EXT1__
'.
However, you appear to be working on Windows (judging from 'tail.exe', and also the non-standard '_get_errno()
'). The rules there may depend on the C compiler you are using.
You should be able to chase down the information from this MSDN article on 'Security Enhancements in the CRT'. My impression was that it should be defined unless you actively suppress the feature, so check out whether you are actively suppressing it in your compilations.
Be aware that the MSVC definition of functions such as vsnprintf_s()
do not match the TR24731-1 definitions:
MSDN:
int vsnprintf_s(
char *buffer,
size_t sizeOfBuffer,
size_t count,
const char *format,
va_list argptr
);
TR 24731-1:
int vsnprintf_s(
char * restrict s,
rsize_t n,
const char * restrict format,
va_list arg
);
The difference is not just a question of type aliases or qualifiers (rsize_t
, restrict
) - there are two sizes in the MS version and one in the Standard version. So much for standardization!
精彩评论