C语言实现修改文件中指定字符串哪一行内容
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
/*参数: filepath - 要修改的文件 str - 修改为的字符串 */
void ModifyFile(char* filepath, char *str)
{
char linebuffer[512] = {0};
char buffer1[512] = {0};
char buffer2[512] = {0};
//char buf[512] = {0};
char cmd[256] = "rm temp.txt";
FILE *fp = fopen(filepath, "r");
FILE *fpttmp = fopen("temp.txt", "wt+");
while(fgets(linebuffer, 512, fp))
{
sscanf(linebuffer, "%[^_]_%[^_]", buffer1,buffer2);
if(!strcmp("11", buffer1))
{
memset(linebuffer, '\0', strlen(linebuffer)+1);
memcpy(linebuffer, str, strlen(str)+1);
//linebuffer[7] = '\0';
fprintf(fpttmp, "%s", linebuffer);
fprintf(fpttmp,"\n");
}
else
{
fprintf(fpttmp, "%s", linebuffer);
}
}
fclose(fp);
fclose(fpttmp);
//清空文件中的内容
fp = fopen(filepath, "w");
fclose(fp);
fp = fopen(filepath, "wt+");
fpttmp = fopen("temp.txt", "r");
while(fgets(linebuffer, 512, fpttmp))
{
fprintf(fp, "%s", linebuffer);
}
fclose(fp);
fclose(fpttmp);
system(cmd);
}
int main()
{
char name[128] = "wugaoquan";
ModifyFile("mm.txt", name);
return 0;
}