user-0 提问: 怎么实现文件中字符串查找替换功能

回复:
user: user-1
content: 所谓修改删除文件a某位置的内容，其实是读打开文件a，再将‘a中修改删除位置之前的内容＋修改删除的内容＋a中修改删除位置之后的内容’保存到文件b，关闭文件a，删除文件a，将文件b改名为与之前文件a相同的名字，仅此而已。http://bbs.csdn.net/topics/391975224

user: user-1
content: 仅供参考：#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN  40 #define MAX2N 80 FILE *fi,*fo; char sn[_MAX_PATH],dn[_MAX_PATH],f[MAX2N],bw[MAXN],bf[MAXN+sizeof(int)-1],bx[MAXN],br[MAXN+sizeof(int)-1],bb[MAXN]; char *ag[MAXN]; int L,i,j,k,lenf,lenr,find,match; __int64 off; void FR() {     printf("\n");     printf("Src : %s\n",sn);     if (find==0) {         printf("Dest: %s\n",dn);     }     for (i=0;i<lenf;i++) {         bb[i]=fgetc(fi);     }     off=0i64;     while (1) {         match=1;         for (j=0;j<lenf;j++) {             if (bw[j]==0&&bf[j]==bb[j]) continue;             if (bw[j]==1) continue;             match=0;             break;//         }         if (match==1) {             printf("%016I64X\n",off);             if (find==0) {                 for (j=0;j<lenf;j++) {                     if (bx[j]==0) fputc(br[j],fo);                     else fputc(bb[j],fo);                 }             }             for (i=0;i<lenf;i++) {                 bb[i]=fgetc(fi);                 if (feof(fi)) break;//             }             if (feof(fi)&&find==0) {                 for (j=0;j<i;j++) fputc(bb[j],fo);                 break;//             }             if (feof(fi)) break;//             else off+=(__int64)lenf;         } else {             if (find==0) {                 fputc(bb[0],fo);             }             memmove(bb,bb+1,lenf-1);             bb[lenf-1]=fgetc(fi);             off++;             if (feof(fi)) {                 if (find==0) {                     for (j=0;j<lenf-1;j++) fputc(bb[j],fo);                 }                 break;//             }         }     } } int main() {     while (1) {         printf("Find & Replace, designed by zhao4zhong1@163.com\n");         printf("Src  file name:");         fflush(stdout);         fgets(sn,_MAX_PATH,stdin);         if ('\n'==sn[0]) break;//         L=strlen(sn);if ('\n'==sn[L-1]) sn[L-1]=0;         if ((fi=fopen(sn,"rb"))==NULL) {             printf("Can not open %s!",sn);             break;//         }         while (1) {             printf("Dest file name:");             fflush(stdout);             fgets(dn,_MAX_PATH,stdin);             if ('\n'==dn[0]) break;//             L=strlen(dn);if ('\n'==dn[L-1]) dn[L-1]=0;             if (strcmpi(sn,dn)==0) printf("Error: Src file name[%s] == Dest file name[%s]!\n",sn,dn);             else if ((fo=fopen(dn,"rb"))!=NULL) {                 fclose(fo);                 printf("Error: Dest file already exists!\n");             } else break;//         }         printf("Find   :");         fflush(stdout);         fgets(f,MAX2N,stdin);         L=strlen(f);if ('\n'==f[L-1]) f[L-1]=0;         if (f[0]=='\'') {             lenf=strlen(f)-1;             for (j=0;j<lenf;j++) {                 if (f[j+1]=='?') bw[j]=1;                 else {                     bw[j]=0;                     bf[j]=f[j+1];                 }             }         } else {             i=0;             for (j=0;j<MAXN;j++) {                 if (f[i]==0x0D||f[i]==0x0A||f[i]==0x1A||f[i]==0) {                     f[i]=0;                     break;//                 }                 while (f[i]==' '||f[i]==9) {                     f[i]=0;                     i++;                 }                 ag[j]=f+i;                 while (f[i]!=9&&f[i]!=' '&&f[i]!=0x0D&&f[i]!=0x0A&&f[i]!=0x00) i++;             }             lenf=j;             for (i=0;i<lenf;i++) {                 if (isxdigit(ag[i][0])) {                     bw[i]=0;                     sscanf(ag[i],"%2x",&bf[i]);                 } else {                     bw[i]=1;                 }             }         }         printf("Replace:");         fflush(stdout);         fgets(f,MAX2N,stdin);         L=strlen(f);if ('\n'==f[L-1]) f[L-1]=0;         if (f[0]==0 || '\n'==dn[0]) find=1;         else {             if (f[0]=='\'') {                 lenr=strlen(f)-1;                 if (lenr!=lenf) {                     printf("Error: Find len != Replace len");                     fclose(fi);                     break;//                 }                 find=0;                 fo=fopen(dn,"wb");                 for (j=0;j<lenr;j++) {                     if (f[j+1]=='?') bx[j]=1;                     else {                         bx[j]=0;                         br[j]=f[j+1];                     }                 }             } else {                 i=0;                 for (j=0;j<MAXN;j++) {                     if (f[i]==0x0D||f[i]==0x0A||f[i]==0x1A||f[i]==0) {                         f[i]=0;                         break;//                     }                     while (f[i]==' '||f[i]==9) {                         f[i]=0;                         i++;                     }                     ag[j]=f+i;                     while (f[i]!=9&&f[i]!=' '&&f[i]!=0x0D&&f[i]!=0x0A&&f[i]!=0x00) i++;                 }                 lenr=j;                 if (lenr!=lenf) {                     printf("Error: Find len != Replace len");                     fclose(fi);                     break;//                 }                 find=0;                 fo=fopen(dn,"wb");                 for (i=0;i<lenr;i++) {                     if (isxdigit(ag[i][0])) {                         bx[i]=0;                         sscanf(ag[i],"%2x",&br[i]);                     } else {                         bx[i]=1;                     }                 }             }         }         if (lenf<=0) break;//         FR();         fcloseall();     }     return 0; }

user: user-2
content: 替换字符串中的特定字符串  void Substitute(char *pInput, char *pOutput, char *pSrc, char *pDst)   {     char    *pi, *po, *p;     int     nSrcLen, nDstLen, nLen;        // 指向输入字符串的游动指针.     pi = pInput;         // 指向输出字符串的游动指针.     po = pOutput;     // 计算被替换串和替换串的长度.     nSrcLen = strlen(pSrc);     nDstLen = strlen(pDst);        // 查找pi指向字符串中第一次出现替换串的位置,并返回指针(找不到则返回null).        p = strstr(pi, pSrc);     if(p)     {             // 找到.         while(p)         {             //计算被替换串前边字符串的长度.             nLen = (int)(p - pi);             // 复制到输出字符串.             memcpy(po, pi, nLen);             memcpy(po + nLen, pDst, nDstLen);             // 跳过被替换串.             pi = p + nSrcLen;             // 调整指向输出串的指针位置.             po = po + nLen + nDstLen;             // 继续查找.             p = strstr(pi, pSrc);         }         // 复制剩余字符串.         strcpy(po, pi);     }     else     {         // 没有找到则原样复制.         strcpy(po, pi);     }   }     测试程序：   int main()   {          FILE *fpSrc,*fpDes;       char filename1[20]="ding1.txt";       char filename2[20]="ding2.txt";       //要求查找的字符串，替换的字符串；       char ps[]="computer";           char pd[]="basketballs";       //求取所查找和替换的字符串的长度；           int len_src=strlen(ps);       int len_des=strlen(pd);       //定义存储字符串缓冲区；很奇怪的一点是，fgets函数不能将字符串写入动态分配的内存中       /*char* Src_buf=(char*)(sizeof(char)*MAXSIZES);       char* Cpy_buf=(char*)(sizeof(char)*MAXSIZES);           char* Des_buf=(char*)(sizeof(char)*MAXSIZES);*/       char Src_buf[MAXSIZES];       char Cpy_buf[MAXSIZES];           char Des_buf[MAXSIZES];           //打开文件       if((fpSrc=fopen(filename1,"r+"))==NULL)       {           printf("fail to open the file1 !\n");          exit(1);        }       if((fpDes=fopen(filename2,"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,ps,pd);           fputs(Des_buf,fpDes);           printf("%s",Des_buf);       }       fclose(fpSrc);       fclose(fpDes);          system("pause");       return 0;   }

user: user-3
content: 参看：http://blog.csdn.net/fyyy4030/article/details/8511476