C语言如何删除文件中的指定行--更新txt等形式文件

基于我的上一篇文章, 链接如下:

如何实现结构体和文件之间的转化

 有时候我们需要删除文件的某一行,来更新文件,在这我个人扩展了一个函数,以删除指定条件的行。

代码如下:

static void UpdateHistoryFile(void)
{FILE *fin,*fout;int counter = 1;STest test;do{fin=fopen("test.txt","r");fout=fopen("t.tmp","w");while(readHistoryFile(fin, &test) != EOF){if (test.b == 20160516) // to add the judgement case.{printf("test.b < 20160516\n");}else{test.a = counter;writeHistoryFile(fout, test);counter++;}}fclose(fin);fclose(fout);remove("test.txt");rename("t.tmp","test.txt");}while(0);return;
}

Best Wishes!

Yannick Tong