ZKX's LAB

c语言 文件 字符串替换 在 C语言中字符串的替换如何实现的!

2020-10-01知识5

C语言文件中字符串的查找与替换 #includeincludeincludeincludeusing namespace std;int Count=0;函数名:findNum作者:anglecloudy描述:如果存在则返回字符串所在的位置,否则返回0,暂不支持文本中存在多个相同的串先用test.txt文本测试,所有的文本操作都是一样的,不管你怎么命名int findNum(char*str){FILE*p;if((p=fopen(\"test.txt\",\"rb\"))=NULL){printf(\"\\n打开文件失败\\n\");return 0;}char buffer[0x1000];保存文件memset(buffer,0,0x1000);初始化缓存size_t fileLen=fread(buffer,sizeof(char),0x1000,p);得到文件内容,int readLen=strlen(str);int IsFind=0;for(int i=0;i;i+){if(strncmp(buffer+i,str,readLen)=0){IsFind=i;}}fclose(p);return IsFind;}int main(void){char*str1=\"1234567;int t1=0,t2=0;if((t1=findNum(str1))=0){printf(\"没有找到字符串%s\\n请按任意键退出\\n\",str1);return-1;}else{printf(\"字符串%s的位置在%d\\n\",str1,t1);}return 0;}我只是简单的改了一下你的字符串查找这个函数,其它的没写。主要是你的思想不对,对文件的操作一般先定义一个数组,把文件保存起来,然后再操作,多去http://community.csdn.net上面问问,高手多,。

c语言编程替换文件中字符串 /*文件字符串替换实用程序,算法简练有效*/编译环境vc2005/dev-c++includeincludeincludedefine 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;i+){for(j=0;j(s[i+j]=s1[j]);j+);if(j。len1)continue;不相等则i加1进行下一次外循环*/else{ for(k=0;k;k+)/*pos记录开始复制的位置*/t[m+]=s[pos+k];pos=i+len1;for(k=0;k;k+)/*字符串替换*/t[m+]=s2[k];}}for(k=pos;k;k+)/*复制剩余字符*/t[m+]=s[k];max=m。

C语言文件中字符串的查找与替换? void Substitute(char*pInput,char*pOutput,char include include include include int Count=0;int findNum(char*str) { int i=0,t1=0;FILE*p;char ch;。

如何用C语言在文件中查找并替换指定字符串?清高手指点。将修改过后的结果保存在新的文本中 编译环境vc2005/dev-c++#include<;stdio.h>;#include<;stdlib.h>;#include<;string.h>;#。

#str函数#strlen#c语言#char函数#include

随机阅读

qrcode
访问手机版