我必须在posix中设置open()的返回值。如Linux手册页所述,如何返回“新文件描述符”:
返回值
open(), openat(), and creat() return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately).
编辑:感谢goldilocks!我没有朝正确的方向看。我正在做的是我正在纠正此系统调用的返回值。显然,它返回了错误的消息。
要打开文件,请使用以下结构:
int fd;
if ((fd = open(path, flags)) < 0) {
/* An error occurred, the reason is in errno */
int _errno = errno; /* Save errno value */
fprintf(stderr, "Failed opening file '%s': %s\n", path, strerror(_errno));
return;
}
/* The file was successfully opened */
因此,您只有一个返回值,通常是文件描述符。如果为-1
,则表明发生了错误。发生的错误存储在errno
变量中(您可以通过将其包含errno.h
到源文件中来获取该变量)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句