int main()
{
FILE* pf = fopen("tate.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
int ch = fgetc(pf);
printf("%c\n", ch);//默认从起始位置开始a
fseek(pf, 1, SEEK_SET);//设置从起始位置开始偏移1
ch = fgetc(pf);//每次用feek设置后必须重新定义ch
printf("%c\n", ch);
fseek(pf, -3, SEEK_END);//从最后开始i
ch = fgetc(pf);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}
ftell
当前文件指针的偏移量
int main()
{
FILE* pf = fopen("tate.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
int ch = fgetc(pf);
fseek(pf, -4, SEEK_END);
ch = fgetc(pf);
int pos = ftell(pf);
printf("%c\n", ch);
printf("%d\n", pos);
fclose(pf);
pf = NULL;
return 0;
}
rewind是文件指针回到文件起始位置
int main()
{
FILE* pf = fopen("tate.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fseek(pf, -4, SEEK_END);
int ch = fgetc(pf);
printf("%c\n", ch);
rewind(pf);
ch = fgetc(pf);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}