请教C语言语的高手

来源:百度知道 编辑:UC知道 时间:2024/04/29 01:24:35
在文件的读取的时候能不能从中间的某一个指定的位子开始读取?

可以用fseek函数

可以的,调用相关的函数即可。

int fseek(FILE *fp, LONG offset, int origin)
设定文件操作指示器位置

fp 文件指针
offset 相对于origin规定的偏移位置量
origin 指针移动的起始位置,可设置为以下三种情况:
SEEK_SET 文件开始位置
SEEK_CUR 文件当前位置
SEEK_END 文件结束位置

例子:
#include <stdio.h>
long filesize(FILE *stream);
int main(void)
{
FILE *stream;
stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("Filesize of MYFILE.TXT is %ld bytes
", filesize(stream));
fclose(stream);
return 0;
}
long filesize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}

debug调试

当然可以,上面大哥说得对