2

我有一个包含多个字节的长二进制字符串,我需要使用它fseek来获取字符串中的特定字节。

我知道我需要计算偏移量,但我不确定偏移量是按位还是按字节计算。例如,如果我需要到达第 3 个字节,我需要将指标推进 3 或 (3*8=)24?

4

2 回答 2

1

fseek将偏移量作为字节数,而不是位:

新位置(从文件开头开始以字节为单位)应通过将偏移量添加到 wherece 指定的位置来获得。(重点是我的)。

于 2013-09-28T15:35:08.080 回答
0
   int fseek(FILE *stream, long offset, int whence);

fseek()函数为 stream 指向的流设置文件位置指示符。 新位置measured in bytes是通过将offset字节添加到 指定的位置来获得的whence

如果 wherece 设置为SEEK_SET, SEEK_CUR, or SEEK_END,则偏移量start of the file, the current position indicator, or end-of-file分别相对于 。

如果您正在使用文本文件

First Byte position  ==>  fseek(fp,0,SEEK_SET);
Second Byte position ==>  fseek(fp,1,SEEK_SET);
Third Byte position  ==>  fseek(fp,2,SEEK_SET); 

您只需要指定Number of bytenot Number of byte * 8

于 2013-09-28T15:41:42.000 回答