我正在研究 unix 系统调用
。
在我的代码中,我想打开
文件并对该文件执行lseek
操作。
请查看以下代码。
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
int fd;
fd = open("testfile.txt", O_RDONLY);
if(fd < 0 );
printf("problem in openning file \n");
if(lseek(fd,0,SEEK_CUR) == -1)
printf("cant seek\n");
else
printf("seek ok\n");
exit(0);
}
我的输出是:
problem in openning file
seek ok
我的问题是:
1) 为什么 open
系统调用给我负文件描述符? (我确认testfile.txt文件在同一个目录下)
2) 在这里我无法打开文件(因为 open()
返回负文件描述符),lseek
如何在不打开文件的情况下成功?
最佳答案
其实你打开文件成功了。
只是if(fd < 0 );
是错误的,您需要删除 ;
关于c++ - 无法 `open` 文件,但 `lseek` 已完成且没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18616678/