文本替換程序代碼

本文是一個文本替換程序的源代碼,代碼只對ANSI做處理。

 

#include <stdio.h> #include <stdlib.h> #include <string> using namespace std; #define BLOCKSIZE 10 void ReplaceMemMore(char* buff,char* src,char* dest); void ReplaceMemLess(char* buff,char* src,char* dest); void ReplaceFileMore(const wchar_t* path,const wchar_t* tempPath,char* src,char* dest); void ReplaceFileLess(const wchar_t* path,const wchar_t* tempPath,char* src,char* dest); void ReplaceFile(wstring path,char* src,char* dest); int NumOfStr(char* buff,char* src); void main() {  wchar_t buff[256];  char src[16];  char dest[16];  memset(buff,0,sizeof(wchar_t)*256);  memset(src,0,sizeof(char)*16);  memset(dest,0,sizeof(char)*16);

 printf("please input your file path(ANSI)\n");  wscanf(L"%s",buff);  printf("please input Src character \n");  scanf("%s",src);  printf("please input dest character \n");  scanf("%s",dest);

 wstring path(buff);  ReplaceFile(path,src,dest);

} ////////////////////////////////////////////////////////////////////////////////////// //函數功能:用給定的字符串dest替換文件path中的字符串src //函數參數:path,文件路徑;src,被替換的字符串,dest,替換成的字符串 //函數返回:無 //函數說明:2011-11-30 HRX ////////////////////////////////////////////////////////////////////////////////////// void ReplaceFile(wstring path,char* src,char* dest) {  wstring pathTemp=path.substr(0,path.rfind(L'\\'))+L"\\temp.txt";

 const wchar_t* pPathTemp=pathTemp.c_str();  const wchar_t* pPath=path.c_str();  if (strlen(dest)>strlen(src))  {   ReplaceFileMore(pPath,pPathTemp,src,dest);  }  else  {   ReplaceFileLess(pPath,pPathTemp,src,dest);  }  _wremove(pPath);  _wrename(pPathTemp,pPath); } ////////////////////////////////////////////////////////////////////////////////////// //函數功能:用給定的字符串dest替換文件path中的字符串src,當dest長度小於src時調用 //函數參數:path,文件路徑;src,被替換的字符串,dest,替換成的字符串;tempPath,文件暫存路徑 //函數返回:無 //函數說明:2011-11-30 HRX ////////////////////////////////////////////////////////////////////////////////////// void ReplaceFileLess(const wchar_t* path,const wchar_t* tempPath,char* src,char* dest) {  FILE* fr=_wfopen(path,L"rb");  FILE* fw=_wfopen(tempPath,L"wb");  int lenSrc=strlen(src);  int lenDest=strlen(dest);  int dist=lenSrc-lenDest;  int n=0;  char buff[BLOCKSIZE+1];  memset(buff,0,sizeof(char)*(BLOCKSIZE+1));  char* pBuff=NULL;  while(fread(buff,1,BLOCKSIZE,fr))  {   n=NumOfStr(buff,src);   if (n==0)   {    for (int i=0;i<BLOCKSIZE;++i)    {

    if(*(buff+i)==0) break;     fputc(*(buff+i),fw);    }   }   else   {    ReplaceMemLess(buff,src,dest);    for (int i=0;i<BLOCKSIZE;++i)    {

    if(*(buff+i)==0) break;     fputc(*(buff+i),fw);    }   }   //fseek(fr,1-strlen(src),SEEK_CUR);   memset(buff,0,sizeof(char)*BLOCKSIZE);  }  fclose(fr);  fclose(fw);

} ////////////////////////////////////////////////////////////////////////////////////// //函數功能:用給定的字符串dest替換文件path中的字符串src,當dest長度大於src時調用 //函數參數:path,文件路徑;src,被替換的字符串,dest,替換成的字符串;tempPath,文件暫存路徑 //函數返回:無 //函數說明:2011-11-30 HRX ////////////////////////////////////////////////////////////////////////////////////// void ReplaceFileMore(const wchar_t* path,const wchar_t* tempPath,char* src,char* dest) {  FILE* fr=_wfopen(path,L"rb");  FILE* fw=_wfopen(tempPath,L"wb");  int lenSrc=strlen(src);  int lenDest=strlen(dest);  int dist=lenDest-lenSrc;  int n=0;  char buff[BLOCKSIZE];  memset(buff,0,sizeof(char)*BLOCKSIZE);  char* pBuff=NULL;  while(fread(buff,1,BLOCKSIZE,fr))  {   n=NumOfStr(buff,src);   if (n==0)   {    for (int i=0;i<BLOCKSIZE;++i)    {

    if(*(buff+i)!=0) fputc(*(buff+i),fw);    }   }   else   {    pBuff=(char*)malloc(BLOCKSIZE+n*dist);    memset(pBuff,0,BLOCKSIZE+n*dist);    if(fseek(fr,-BLOCKSIZE,SEEK_CUR)==-1)fseek(fr,0,SEEK_SET);    fread(pBuff,1,BLOCKSIZE,fr);    ReplaceMemMore(pBuff,src,dest);    for (int i=0;i<(BLOCKSIZE+n*dist);++i)    {

    if(*(pBuff+i)!=0) fputc(*(pBuff+i),fw);    }    free(pBuff);   }   //fseek(fr,1-strlen(src),SEEK_CUR);   memset(buff,0,sizeof(char)*BLOCKSIZE);  }  fclose(fr);  fclose(fw);

} ////////////////////////////////////////////////////////////////////////////////////// //函數功能:用給定的字符串dest替換字符串buff中的字符串src,當dest長度大於src時調用 //函數參數:buff,要查找的字符串;src,被替換的字符串,dest,替換成的字符串; //函數返回:無 //函數說明:2011-11-30 HRX ////////////////////////////////////////////////////////////////////////////////////// void ReplaceMemMore(char* buff,char* src,char* dest) {  int LenSrc=strlen(src);  int LenDest=strlen(dest);  int Dist=LenDest-LenSrc;  char* temp=NULL;//指向最後一個字符\0  while (buff=strstr(buff,src))  {   temp=buff+strlen(buff);   while (temp!=(buff+Dist-1))   {//字符後退'差'個空間    *(temp+Dist)=*temp;    temp--;   }   for (int i=0;i<LenDest;i++)   {//給空出的空間賦值    *(buff+i)=*(dest+i);   }   buff=buff+LenDest;  } } ////////////////////////////////////////////////////////////////////////////////////// //函數功能:用給定的字符串dest替換字符串buff中的字符串src,當dest長度小於src時調用 //函數參數:buff,要查找的字符串;src,被替換的字符串,dest,替換成的字符串; //函數返回:無 //函數說明:2011-11-30 HRX ////////////////////////////////////////////////////////////////////////////////////// void ReplaceMemLess(char* buff,char* src,char* dest) {

 int LenSrc=strlen(src);  int LenDest=strlen(dest);  int Dist=LenSrc-LenDest;  char* temp=NULL;//指向最後一個字符\0  while (buff=strstr(buff,src))  {   temp=buff;   while (*temp!=0)   {//字符後退'差'個空間    *temp=*(temp+Dist);    ++temp;   }   for (int i=0;i<LenDest;i++)   {//給空出的空間賦值    *(buff+i)=*(dest+i);   }   buff=buff+LenDest;  } } ////////////////////////////////////////////////////////////////////////////////////// //函數功能:計算字符串buff中含有字符串src的個數 //函數參數:buff,要查找的字符串;src,被查找的字符串; //函數返回:int;buff中含有src的個數 //函數說明:2011-11-30 HRX ////////////////////////////////////////////////////////////////////////////////////// int NumOfStr(char* buff,char* src) {  int n=0;  int len=strlen(src);  while (buff=strstr(buff,src))  {   ++n;   buff+=len;  }  return n; }

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章