void file_update_test()
{
FILE* fp;
char buf[1024] = { 0 };
fp = fopen("1.txt","rb+");
int update_index = 2;
int cnt = 0;
if (fp == NULL)
{
printf("open fail");
return;
}
while (fgets(buf, sizeof(buf), fp))
{
cnt++;
printf("buf=%s\n", buf);
if (update_index == cnt)
{
fseek(fp, -strlen(buf), SEEK_CUR); // 把文件指针移动回当前行的开头
fprintf(fp, "202020");
break;
}
}
fclose(fp);
}
标签:fp,cnt,int,update,C语言,修改,按行,buf
From: https://www.cnblogs.com/xiaohai123/p/17322601.html