69,372
社区成员
发帖
与我相关
我的任务
分享
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char path[100] = "E:\\1.txt";
char temppath[100] = { 0 }; //定义临时文件的路径
FILE *pf = fopen(path, "r"); //按照读的方式打开文件
if (pf == NULL)
{
printf("文件打开失败");
return;
}
tmpnam(temppath); //产生一个唯一的文件名
FILE *ptemp = fopen(temppath, "w");
if (ptemp == NULL)
{
printf("临时文件创建失败");
return;
}
char str[120];
char findstr[30] = "换";
while (fgets(str, 120, pf)) //从原来的文件读取字符串
{
char *p = strstr(str, findstr);
if (p == NULL) //没有找到
{
fputs(str, ptemp); //正常写入临时文件
}
else
{
//找到 //什么都不操作
}
}
fclose(pf);
fclose(ptemp);
char show[200];
sprintf(show, "type %s", temppath);
system(show);
if (remove(path) == 0)
{
printf("Removed %s", path);
}
else
{
perror("remove");
}
//remove(path); //删除原来的文件
rename(temppath, path); //将临时文件命名为原来的文件
system("pause");
}