c语言编程替换文件中字符串

来源:百度知道 编辑:UC知道 时间:2024/07/02 01:55:41
实现记事本替换功能:有一个文件a1.txt里面存有大量的字符,现在需要替换其中的一些字符,用户输入要替换的字符串,和被替换成的字符串,例如输入:
aabac
abc
则如果a1.txt中有字符串aabac就会被替换成abc
谁能写出符合题意的源程序,我给他加50分

/*文件字符串替换实用程序,算法简练有效*/
编译环境vc2005/dev-c++
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 10000 /* 设定文件最大字符个数不超过10000,可更改 */

int main()
{
int i=0,j,k=0,m=0,pos=0,max,len1,len2;
char s[N],t[N],a[200],s1[200],s2[200];
FILE *fp;
printf("Please input file name:\n");/*输入文件名*/
gets(a);
printf("Please input original string:\n");/*输入要替换的字符串*/
gets(s1);
printf("Please input new string:\n");/*输入被替换成的字符串*/
gets(s2);
len1=strlen(s1);
len2=strlen(s2);
if ((fp=fopen(a,"r"))==NULL)/*设定文件位于当前目录下,可更改为绝对路径*/
{ printf("Open file %s error! Strike any key to exit!",a);
system("pause");
exit(1);
}
s[i++]=fgetc(fp);
while(!feof(fp))
{s[i++]=fgetc(fp);
}
fclose(fp);

max=i-1;/* 函数feof()最后会读两次,所以必须减1 */
for(i=0;i<