有时候我们需要删除文件的某一行,来更新文件,在这我个人扩展了一个函数,以删除指定条件的行。
1 static void UpdateHistoryFile(void) 2 { 3 FILE *fin,*fout; 4 int counter = 1; 5 STest test; 6 7 do 8 { 9 fin=fopen("test.txt","r"); 10 fout=fopen("t.tmp","w"); 11 12 while(readHistoryFile(fin, &test) != EOF) 13 { 14 if (test.b == 20160516) // to add the judgement case. 15 { 16 printf("test.b < 20160516 "); 17 } 18 else 19 { 20 test.a = counter; 21 writeHistoryFile(fout, test); 22 counter++; 23 } 24 } 25 26 fclose(fin); 27 fclose(fout); 28 29 remove("test.txt"); 30 rename("t.tmp","test.txt"); 31 }while(0); 32 33 return; 34 }