C语言文本文件中字符串的查找与替换。
在文本文件中查找并替换字符串。命令格式为:replaceStringfilenamestring1string2,将文件filename中所有string1用string...
在文本文件中查找并替换字符串。命令格式为:replaceString filename string1 string2,将文件filename中所有string1用string2替换。
展开
3个回答
展开全部
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
char filename[10],string1[15],string2[15],line[100];
FILE *pfile=NULL;
scanf("%s %s %s",filename,string1,string2);
pfile=fopen(filename,"r+");
if(!pfile)
{
perror("文件不存在");
return 1;
}
printf("将把文件%s中字符串%s替换成%s\n",filename,string1,string2);
while(!feof(pfile))
{
char *index=NULL;
fgets(line,100,pfile);
index=strstr(line,string1);
if(index)
{
int d2=strlen(string2);
int d1=strlen(string1);
printf("%s 中有%s\n",line,string1);
if(d1!=d2)
{
memmove(
index+d1+d2-d1,
index+d1,
strlen(line));
}
memcpy(index,string2,strlen(string2));
fseek(pfile,-strlen(line)+d2-d1,SEEK_CUR);
fputs(line,pfile);
fflush(pfile);
}
}
fclose(pfile);
return 0;
}
展开全部
strstr函数应该满足你的要求。试试吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*
功能:查找替换函数
返回值:替换后的字符串 strretu
*/
char *Replace(char *str, char *str1, char *str2, char *strretu)
{
char *str_temp = str; //源字符串
char *str1_temp = str1; //要求替换的字符串
char *str2_temp = str2; //替换成字符串
char *strretu_temp = NULL;//目标字符串
unsigned long char_size = (unsigned long)sizeof(char);
/*计算字符长度*/
unsigned long i,j,rep_acc = 0;
unsigned long str_len = strlen(str);
unsigned long str1_len = strlen(str1);
unsigned long str2_len = strlen(str2);
/*首先找出Str里有几个地方要被替换的*/
for(; str_temp - str < str_len; )
{
for(i = 0; i < str1_len; i++)
{
if(*str_temp++ != *str1_temp++)
{
str1_temp = str1;
break;
}
else
{
if(i == str1_len - 1)
{
str1_temp = str1;
rep_acc++;//找到了
}
}
}
}
str1_temp = str1;
str_temp = str;
/*分配所需内存*/
strretu = strretu_temp = malloc(((str_len - str1_len * rep_acc) + str2_len * rep_acc + 1) * char_size);
/*替换*/
for(; str_temp - str < str_len; )
{
for(i = 0; i < str1_len; i++)
{
*strretu_temp++ = *str_temp;
if(*str_temp++ != *str1_temp++)
{
str1_temp = str1;
break;
}
else
{
if(i == str1_len - 1)
{
strretu_temp = strretu_temp - str1_len;
for(i = 0; i < str2_len; i++)
{
*strretu_temp++ = *str2_temp++;
}
str2_temp = str2;
str1_temp = str1;
}
}
}
}
*strretu_temp = '\0';
str1_temp = str1;
str_temp = str;
str2_temp = str2;
return(strretu);
}
功能:查找替换函数
返回值:替换后的字符串 strretu
*/
char *Replace(char *str, char *str1, char *str2, char *strretu)
{
char *str_temp = str; //源字符串
char *str1_temp = str1; //要求替换的字符串
char *str2_temp = str2; //替换成字符串
char *strretu_temp = NULL;//目标字符串
unsigned long char_size = (unsigned long)sizeof(char);
/*计算字符长度*/
unsigned long i,j,rep_acc = 0;
unsigned long str_len = strlen(str);
unsigned long str1_len = strlen(str1);
unsigned long str2_len = strlen(str2);
/*首先找出Str里有几个地方要被替换的*/
for(; str_temp - str < str_len; )
{
for(i = 0; i < str1_len; i++)
{
if(*str_temp++ != *str1_temp++)
{
str1_temp = str1;
break;
}
else
{
if(i == str1_len - 1)
{
str1_temp = str1;
rep_acc++;//找到了
}
}
}
}
str1_temp = str1;
str_temp = str;
/*分配所需内存*/
strretu = strretu_temp = malloc(((str_len - str1_len * rep_acc) + str2_len * rep_acc + 1) * char_size);
/*替换*/
for(; str_temp - str < str_len; )
{
for(i = 0; i < str1_len; i++)
{
*strretu_temp++ = *str_temp;
if(*str_temp++ != *str1_temp++)
{
str1_temp = str1;
break;
}
else
{
if(i == str1_len - 1)
{
strretu_temp = strretu_temp - str1_len;
for(i = 0; i < str2_len; i++)
{
*strretu_temp++ = *str2_temp++;
}
str2_temp = str2;
str1_temp = str1;
}
}
}
}
*strretu_temp = '\0';
str1_temp = str1;
str_temp = str;
str2_temp = str2;
return(strretu);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询