简体   繁体   中英

the file descriptor return the wrong value

int fd;
fd=io->fd.open(filename,O_RDONLY|O_BINARY);
string_s=strerror(error);

And the result is confusing.fd=3,and string_s is "no such file or directory". I am sure filename is all right.But why fd is not -1

If a function call doesn't fail, the value of errno is undefined . You must check if the function failed before you check errno .

errno is never cleared by any library call. The C11 standard draft n1570 says the following ( 7.5p3 ):

The value of errno in the initial thread is zero at program startup (the initial value of errno in other threads is an indeterminate value), but is never set to zero by any library function . The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this International Standard.

That is, the errno has a non-zero value from a previous failed system call; the open succeeded.

You can always set errno to 0 explicitly yourself:

errno = 0;
fd = io->fd.open(filename, O_RDONLY | O_BINARY);

and it should remain 0 after a successful library call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM