C 密桃成孰时1997国语版 文件中 密桃成孰时1997国语版 的查找与替换
#includestdio.h
#includeconio.h
#includestring.h
#includestdlib.h
void Substitute(char *pInput, char *pOutput, char *pSrc, char *pDst)
{
char *pi, *po, *p;
int nSrcLen, nDstLen, nLen;
// 指向输入密桃成孰时1997国语版的游动指针.
pi = pInput;
// 指向输出密桃成孰时1997国语版的游动指针.
po = pOutput;
// 计算被替换串和替换串的长度.
nSrcLen = strlen(pSrc);
nDstLen = strlen(pDst);
// 查找pi指向密桃成孰时1997国语版中第一次出现替换串的位置,并返回指针(找不到则返回null).
p = strstr(pi, pSrc);
if(p)
{
// 找到.
while(p)
{
//计算被替换串前边密桃成孰时1997国语版的长度.
nLen = (int)(p – pi);
// 复制到输出密桃成孰时1997国语版.
memcpy(po, pi, nLen);
memcpy(po + nLen, pDst, nDstLen);
// 跳过被替换串.
pi = p + nSrcLen;
// 调整指向输出串的指针位置.
po = po + nLen + nDstLen;
// 继续查找.
p = strstr(pi, pSrc);
}
// 复制剩余密桃成孰时1997国语版.
strcpy(po, pi);
}
else
{
// 没有找到则原样复制.
strcpy(po, pi);
}
}
int main(int ac, char *av[])
{
if (ac!=5) {
printf(“程序名 要操作的文件 新文件 查找的密桃成孰时1997国语版 替换的密桃成孰时1997国语版\n”);
printf(“示例:test.exe 1.txt 2.txt hello love\n”);
return 0;
}
const int MAXSIZES = 100;
FILE *fpSrc,*fpDes;
char filename1[20]=”1.txt”;
char filename2[20]=”2.txt”;
//要求查找的密桃成孰时1997国语版,替换的密桃成孰时1997国语版;
char ps[]=”hello”;
char pd[]=”love”;
//求取所查找和替换的密桃成孰时1997国语版的长度;
int len_src=strlen(av[3]);
int len_des=strlen(av[4]);
//定义存储密桃成孰时1997国语版缓冲区;很奇怪的一点是,fgets函数不能将密桃成孰时1997国语版写入动态分配的内存中
char Src_buf[MAXSIZES] = {0};
char Cpy_buf[MAXSIZES] = {0};
char Des_buf[MAXSIZES] = {0};
//打开文件
if((fpSrc=fopen(av[1],”r+”))==NULL)
{
printf(“fail to open the file1 !\n”);
exit(1);
}
if((fpDes=fopen(av[2],”a+”))==NULL)
{
printf(“fail to open the file2 !\n”);
exit(1);
}
//进行循环读取
while(!feof(fpSrc))//判断文件是否已结束;!feof(fpSrc)
{
fgets(Src_buf,MAXSIZES,fpSrc);
Substitute(Src_buf,Des_buf,av[3],av[4]);
fputs(Des_buf,fpDes);
printf(“%s”,Des_buf);
}
fclose(fpSrc);
fclose(fpDes);
system(“pause”);
return 0;
}
说明:使用c标准为中的文件I/O函数一般是不推荐的,做为练习可以学习,真正用的最多的是内存文件映射。那要比用I/O函数读来读取方便的多,限制也会更小。
91影院亚洲日韩
strtok,strstr,strchar
是c密桃成孰时1997国语版的基本密桃成孰时1997国语版查找函数,你可以上网上看看解说,其他的都可以用这些组合。
91影院影视app
C密桃成孰时1997国语版实现密桃成孰时1997国语版替换函数:
#include stdio.h
#include stdlib.h
#include ctype.h
#include string.h
//密桃成孰时1997国语版替换函数
char * my_strstr(char * ps,char *pd)
{
char *pt = pd;
int c = 0;
while(*ps != ‘\0’)
{
if(*ps == *pd)
{
while(*ps == *pd *pd!=’\0′)
{
ps++;
pd++;
c++;
}
}else
{
ps++;
}
if(*pd == ‘\0’)
{
return (ps – c);
}
c = 0;
pd = pt;
}
return 0;
}
void * memcpy(void * dest,const void *src,size_t count)
{
char *tmp = (char *) dest, *s = (char *) src;
while (count–)
*tmp++ = *s++;
return dest;
}
int str_replace(char *p_result,char* p_source,char* p_seach,char *p_repstr)
{
int c = 0;
int repstr_leng = 0;
int searchstr_leng = 0;
char *p1;
char *presult = p_result;
char *psource = p_source;
char *prep = p_repstr;
char *pseach = p_seach;
int nLen = 0;
repstr_leng = strlen(prep);
searchstr_leng = strlen(pseach);
do{
p1 = my_strstr(psource,p_seach);
if (p1 == 0)
{
strcpy(presult,psource);
return c;
}
c++; //匹配子串计数加1;
printf(“结果:%s\r\n”,p_result);
printf(“源字符:%s\r\n”,p_source);
// 拷贝上一个替换点和下一个替换点中间的密桃成孰时1997国语版
nLen = p1 – psource;
memcpy(presult, psource, nLen);
// 拷贝需要替换的密桃成孰时1997国语版
memcpy(presult + nLen,p_repstr,repstr_leng);
psource = p1 + searchstr_leng;
presult = presult + nLen + repstr_leng;
}while(p1);
return c;
}
#define MAX 200
int main(void)
{
int i = 0;
char s[MAX] ={0}; //存放源字串
char s1[MAX]={0}; //存放子字串
char s2[MAX]={0}; //存放替换字串
char result_a[2000] = {0};//存放替换结果;
char *p,*ptm,*pr;
puts(“Please input the string for s:”);
scanf(“%s”,s);
puts(“Please input the string for s1:”);
scanf(“%s”,s1);
puts(“Please input the string for s2:”);
scanf(“%s”,s2);
ptm = s;
pr = result_a;
i = str_replace(pr,ptm,s1,s2);
printf(“替换%d个子密桃成孰时1997国语版;\r\n”,i);
printf(“替换后结果:%s\r\n”,result_a);
system(“pause”);
}
91约炮网址最新网址
简单说:
找到一个符合的位置i
从SRC复制子密桃成孰时1997国语版到temp1,长度为i,意思就是把找到的位置前面的先复制过去;
然后拼接dst到temp1;
接着移动src密桃成孰时1997国语版指针到找到的sub之后,
再拼接特征密桃成孰时1997国语版之后的字符到temp1
完成了替换、返回
的确需要自己多尝试,不懂的,搜索,每一种密桃成孰时1997国语版在网络上都有相关网站提供详细的函数说明,剩下的就是去领会算法。
91影院
#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中密桃成孰时1997国语版%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;
}