【发布时间】:2012-05-01 02:04:08
【问题描述】:
我的意思是功能方面。我正在使用 fileno 将 FILE* 转换为 fd 并且它返回时没有任何错误,但是当我对 fileno 的返回值使用 pread 时,它给了我一个错误的文件描述符错误。即:
FILE* fin;
FILE* fout;
int fd, result;
fd = open("path", O_RDWR);
// Do stuff with fin and fout
// fout is the file with all of the stuff I want to copy to the fd
fd = fileno(fout);
result = pread(fd, buf, size, offset); // Bad file descriptor--returns a 9
我不知道是什么原因导致 pread 给我这个错误,这让我发疯了。
【问题讨论】:
-
你不能用
open打开两端吗? -
pread返回 9,还是返回 -1 并将errno设置为 9?如果是前者,那意味着有 9 个字节要读取,这是巧合。如果它返回 -1 并且errno是 9 (EBADF) 那么已经有fclosedfout。 -
你不是说要使用
fout = fopen("path", "rw");吗?您发布的代码显示fout未定义,因此如果它做了任何有用的事情,那就太令人惊讶了。 -
对不起。 pread 返回 -1 并将 errno 设置为 9。这也只是我的代码的一个 sn-p —— 粘贴整个东西会非常非常冗长。至于fd,它是一个文件描述符,所以它不是必须使用open而不是fopen吗?不过,我还没有尝试过使用“open”。
标签: c file file-descriptor