什么这个lseek的之间的差异,fseek的,读,FREAD? [英] Whats the difference between this lseek, fseek, read, fread?

查看:179
本文介绍了什么这个lseek的之间的差异,fseek的,读,FREAD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用这些函数:

unsigned blah[5];
lseek(0, 100, SEEK_CUR);
read(0, blah, sizeof(blah));

FILE *fr;
fr = fopen(arg[1], "r");
unsigned blah[5];
fseek(fr, 100, SEEK_CUR);
fread(blah, 1, sizeof(blah), fr);

和我运行的第一个code我运行此命令:

And I run the first code my running this command:

cat TEXTFILE | ./a.out

我运行第二个code我运行此命令:

I run the second code my running this command:

./a.out TEXTFILE

不过,我得到不同的结果。而第一个正确seeked,所以它读出正确的文本,第二个是不。我想使用第二个格式,所以我做了什么错了?

However, I am getting different results. While the first one seeked properly, so it reads the correct text, the second one is not. I want to use the second format, so what did I do wrong?

推荐答案

随着加入打开(文件名,O_RDONLY)的; 打电话给第一个例子,无论是工作对我罚款。我怀疑你的问题是因为调用 lseek的(0,100,SEEK_CUR)的; ,它要求一个寻求标准输入。你不能总是寻求在标准 - 如果您有:

With the addition of an open(filename,O_RDONLY); call to the first example, both worked fine for me. I suspect your problem is because of the call lseek(0, 100, SEEK_CUR);, which is asking for a seek on standard input. You cannot always seek on standard in - if you have:

 cat file | ./my_program 

那么标准输入是一个FIFO,你可以不求。如果我这样做是我的系统上,力求通过返回 1 ,以非法寻求错误失败。这可能是你遇到的问题,你可能不会注意到,因为你没有在你的例子检查寻求调用的返回值。

Then standard input is a fifo, and you can't seek. If I do this on my system, seek fails by returning -1, with an error of "Illegal seek". This may be the problem you're having, and you might not notice since you don't check the return value of the seek call in your example.

请注意,如果您有:

 ./my_program < file

那么标准输入是一个文件,你可能会寻求。在我的系统,寻求回报 100 ,输出是正确的。

下面是一个程序,你可以用它来说明返回值:

Here is a program you can use to illustrate the return values:

int main(void){ 

  int fd = 0;
  char blah[5];
  printf("Seek moved to this position: %d\n",lseek(fd, 100, SEEK_CUR));
  perror("Seek said");
  printf("Then read read %d bytes\n",read(fd, blah, sizeof(blah)));
  printf("The bytes read were '%c%c%c%c%c'\n",blah[0],blah[1],blah[2],blah[3],blah[4]); 
}                       

和这里有两个执行:

 $ ./a.out < text
 Seek moved to this position: 100
 Seek said: Success
 Then read read 5 bytes
 The bytes read were '+++.-'

(这些都是从位置100在该文件的正确字节)

(Those are the correct bytes from position 100 in that file)

$ cat text | ./a.out 
Seek moved to this position: -1
Seek said: Illegal seek
Then read read 5 bytes
The bytes read were '# def'

(这些字节是第5个字节的文件)

(Those bytes are the first 5 bytes of the file)

我也注意到,标准输入版本是你说的是正确的工作之一。如果您在使用 FILE * 版本的麻烦,我怀疑 fopen()函数调用失败,所以要一定要检查从的fopen的返回值()。当然,你总是可以做到这一点:

I also noticed that the standard input version was the one you said was working correctly. If you're having trouble with the FILE * version, I suspect the fopen() call is failing, so make sure you check the return value from fopen(). Of course, you can always do this:

FILE *fr = stdin;  

所以,你在阅读的标准然而,正如你不能总是在寻求标准,我建议总是打开一个文件,如果你打算寻求。

请注意,你不能在所有设备上寻求可以在打开的文件(尽管你不会对大多数系统的一个问题),所以你应该经常检查的结果求(),以确保它成功了。

Note that you can't seek on all devices that you can open files on (though you won't have a problem on most systems), so you should always check that the result of a seek() to make sure it succeeded.

这篇关于什么这个lseek的之间的差异,fseek的,读,FREAD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆