我的一个库函数中有以下代码,我在循环中多次调用该函数。经过大量迭代后,我发现 open
返回 -1,这是不应该的,因为之前的迭代工作正常。可能是什么原因。我怎样才能获得有关错误的更多详细信息?
int mode;
if (fileLen == 0)
mode = O_TRUNC | O_RDWR | O_CREAT;
else
mode = O_RDWR;
myFilDes = open (fName, mode, S_IRUSR | S_IWUSR);
编辑:每次迭代结束后,我调用库公开的一个方法,该方法在内部调用 close (myFilDes);
最佳答案
perror 是将 errno 映射到字符串并将其打印到 stderr 的标准函数:
if (myFilDes == -1)
perror("Unable to open file: ");
man errno/man perror/man strerror 了解更多信息。
关于C++:文件打开调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5247221/