c语言程序设计 单词替换

来源:百度知道 编辑:UC知道 时间:2024/05/10 20:22:15
1,输入文本文件名,替换前的单词和替换后的单词。读入该文本文件,对指定的单词进行替换,把替换后的内容保存到另一个文件中,并输去替换单词的次数。
2采用模块化思想设计程序,合理定义函数。

请高手指点一下,闲人勿扰,由于程序小,急用请尽快。谢谢!

//查找并替换指定文件所包含的字符串,保存于另一个文件中

#include "stdio.h"
#include "iostream.h"
#include "string.h"

void main()
{
FILE *fp1,*fp2;
char path[128],path_new[128],search[128],replace[128];

cout<<"input the file path and name."<<endl;//输入原文件的路径和文件名
gets(path);
//puts(path);
cout<<"input the new file path and name."<<endl;//输入新文件的路径和文件名
gets(path_new);
cout<<"input the words you want to search."<<endl;//输入所查找的字符串
gets(search);
cout<<"input the words you want to replace."<<endl;//输入要替换的字符串
gets(replace);

int len;//所查找的字符串的长度
len=strlen(search);
//cout<<len<<endl;
int len_replace;//要替换的字符串的长度
len_replace=strlen(replace);

if((fp1=fopen(path,"r+"))==NULL)//打开原文件
{
cout<<"Can not open thi