C++:文件打开调用失败

dev*_*ull 2 c++ linux

我在我的一个库函数中有以下代码,我在循环中多次调用它.在大量迭代之后,我发现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);
Run Code Online (Sandbox Code Playgroud)

编辑:每次迭代结束后,我调用一个方法,库暴露内部调用 close (myFilDes);

kau*_*ppi 5

perror是将errno映射到字符串并将其打印到stderr的标准函数:

if (myFilDes == -1)
    perror("Unable to open file: ");
Run Code Online (Sandbox Code Playgroud)

man errno/man perror/man strerror获取更多信息.