使用 fseek 、 fread 和指针读取文件
Reading files using fseek , fread and pointers
我正在尝试使用 c 来处理图像,并试图完全理解 fseek()
和 fread()
机制。
为什么fseek
虽然影响了fread
功能却没有改变点地址却没有改变或增加点地址?
这是一个简单的例子
int main()
{
char *x[5]={"axxxx","aaaa","hxxx","rrrrr","xsdsdd"};
char *point=x;
char buffer[65]={0};
fread(buffer,6,1,point); //Here fread copy "axxx"
fseek(point,5,SEEK_CUR); //Here fseek increase point by five bytes
fread(buffer,6,1,point); // Here fread do nothing copy nothing
printf("%s\n",buffer); // buffer contain "axxx" first fread call
printf("%x\n",point); // point address did not changed because of fseek
printf("%x",x); //x still the same as point pointer
return 0;
}
fread
应该为第 4 个参数采用 FILE *
参数。你给一个"char *".
您对这里的真正期望是什么?
FILE
是一个包含我们所说的 "position indicator" 的 opact 结构。这就是允许 "move" 在文件中 fseek
和 ftell
.
你的 char *
没有,你的代码有问题。
使用fopen
正确打开文件。
我正在尝试使用 c 来处理图像,并试图完全理解 fseek()
和 fread()
机制。
为什么fseek
虽然影响了fread
功能却没有改变点地址却没有改变或增加点地址?
这是一个简单的例子
int main()
{
char *x[5]={"axxxx","aaaa","hxxx","rrrrr","xsdsdd"};
char *point=x;
char buffer[65]={0};
fread(buffer,6,1,point); //Here fread copy "axxx"
fseek(point,5,SEEK_CUR); //Here fseek increase point by five bytes
fread(buffer,6,1,point); // Here fread do nothing copy nothing
printf("%s\n",buffer); // buffer contain "axxx" first fread call
printf("%x\n",point); // point address did not changed because of fseek
printf("%x",x); //x still the same as point pointer
return 0;
}
fread
应该为第 4 个参数采用 FILE *
参数。你给一个"char *".
您对这里的真正期望是什么?
FILE
是一个包含我们所说的 "position indicator" 的 opact 结构。这就是允许 "move" 在文件中 fseek
和 ftell
.
你的 char *
没有,你的代码有问题。
使用fopen
正确打开文件。