标准io文件指针问题
标准io全缓存
1.文件写完指针在文件尾,需要移动文件指针到行首,才能进行文件读操作。
#include<stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = fopen("./hello","a+");
char ch = 'A';
if(fp != NULL)
{
fputc(ch,fp);
fseek(fd,0,SEEK_SET); //将文件指针移动到文件首
while((ch1 = fgetc(fp)) != EOF)
printf("%c",ch1);
}
printf("\n");
return 0;
}
2.fseek()
(1)功能:
移动文件指针的位置
(2)头文件及函数原型
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);
(3)参数说明:
FILE *stream //fopen的返回值
long offset //偏移量
int whence //基准值,相对位置
//偏移量:正数---》向后移动 负数----》向前移动
fseek(fp,100,SEEK_SET); //相对文件起始位置向后移动100个字符
fseek(fp,-100,SEEK_END); //相对于文件尾位置向前移动100个字符
fseek(fp,100,SEEK_CUR); //相对于当前文件指针的位置向后移动100个字符
fseek(fp,-100,SEEK_CUR); //相对于当前文件指针的位置向前移动100个字符
fseek(fp,0,SEEK_SET); //移动到文件首