现作的, 有bug的话站内短信我
要处理的文件必须先改成英文名( 只含ASCII的名字 )
fstream不能直接打开中文名文件, 而setlcale和locale::global也会造成不能显示或报错( 网上的方法和我自己尝试都不行 ), 我也不想用c或api来代替, 只能凑合一下了; 或者你自己改下代码, 用其他文件打开方式
#include <fstream>
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
class FileWordReplacer
{
protected:
static FileWordReplacer *singleInstance;
ifstream fileIn;
ofstream fileOut;
FileWordReplacer(){}
string& StringReplace( string &szSentence, string wordOld, string wordNew ) const;
string& StringReplaceEx( string &szSentence, string wordOld, string wordNew ) const;
string& StringToUper( string &szSource ) const;
public:
static FileWordReplacer* GetInstance()
{
if( NULL == singleInstance )
return singleInstance = new FileWordReplacer;
return singleInstance;
}
bool FileReplace( string szFileName, string wordOld, string wordNew, bool bDiv );
};
bool FileWordReplacer::FileReplace( string szFileName, string wordOld, string wordNew, bool bDiv )
{
fileIn.rdbuf()->open( szFileName.c_str(), ios::in );
if( !fileIn.is_open() )
{
cerr << "File does'nt exist." << endl;
return false;
}
fileOut.rdbuf()->open( (szFileName + "_").c_str(), ios::out );
string szRead;
while( getline( fileIn, szRead ) )
fileOut
<< (bDiv ? StringReplace( szRead, wordOld, wordNew ) : StringReplaceEx( szRead, wordOld, wordNew ) )
<< endl;
fileIn.rdbuf()->close();
fileOut.rdbuf()->close();
string szCmd = "del " + szFileName;
system( szCmd.c_str() );
szCmd = "ren " + szFileName + "_ ";
size_t nPos = szFileName.find_last_of('\\') + 1;
szCmd += szFileName.substr( nPos, szFileName.size() - nPos );
system( szCmd.c_str() );
return true;
}
string& FileWordReplacer::StringReplace( string &szSentence, string wordOld, string wordNew ) const
{
size_t nPos = 0;
while( string::npos != (nPos = szSentence.find( wordOld, nPos)) )
{
szSentence.replace( nPos, wordOld.size(), wordNew );
nPos += wordNew.size();
}
return szSentence;
}
string& FileWordReplacer::StringReplaceEx( string &szSentence, string wordOld, string wordNew ) const
{
string szUper = szSentence;
StringToUper( szUper );
string wordUper = wordOld;
StringToUper( wordUper );
const size_t nDef = wordNew.size() - wordOld.size();
size_t nPos = 0;
int nTimes = 0;
while( string::npos != (nPos = szUper.find( wordUper, nPos)) )
{
szSentence.replace( nPos + nDef * nTimes, wordOld.size(), wordNew );
nPos += wordUper.size();
nTimes++;
}
return szSentence;
}
string& FileWordReplacer::StringToUper( string &szSource ) const
{
string::iterator it = szSource.begin();
while( szSource.end() != it )
{
if( islower( *it ) )
*it = toupper( *it );
it++;
}
return szSource;
}
FileWordReplacer* FileWordReplacer::singleInstance = NULL;
int main()
{
string szFileName;
cout << "输入文件名 : (可以是绝对路径或相对路径)" << endl;
cin >> szFileName;
string wordOld, wordNew;
cout << "输入想要替换的单词 :" << endl;
cin >> wordOld;
cout << "输入新的单词:" << endl;
cin >> wordNew;
bool bDiv;
while( true )
{
cout << "区分大小写? (Y/N)" << endl;
char c;
cin >> c;
if( 'y' == c || 'Y' == c )
{
bDiv = true;
break;
}
else if( 'n' == c || 'N' == c )
{
bDiv = false;
break;
}
}
if ( FileWordReplacer::GetInstance()->FileReplace( szFileName, wordOld, wordNew, bDiv ) )
cout << "\n替换成功" << endl;
return 0;
}
c语言的, 这个可以用中文文件名
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define TRUE 1
#define FALSE 0
#define NULL 0
#define EOF (-1)
#define BUFFER_SIZE 4 * 1024 //当文件单行超过1k字节时, 增加该值, 是 4 * 最大行长
typedef int BOOL;
typedef struct tagWORD
{
char szValue[256];
int nLength;
}WORD, *PWORD;
typedef struct tagSENTENCE
{
char *szValue;
int nLength;
}SENTENCE, *PSENTENCE;
// 从用户处获取文件名
void GetFileName( char *szFileName );
// 从用户处获取新旧两句单词(短语)
void GetPairWord( PWORD pWordOld, PWORD pWordNew );
// 从用户处咨询是否区分大小写
BOOL IsDiv();
// 将指定文件中的旧单词替换为新单词, 以bDiv为是否区分大小写的标志
void FileWordReplace( const char *szFileName, PWORD pWordOld, PWORD pWordNew, BOOL bDiv );
int main()
{
char szFileName[64];
WORD wordOld, wordNew;
BOOL bDiv;
//用户输入文件名
GetFileName( szFileName );
//用户输入新旧单词
GetPairWord( &wordOld, &wordNew );
//询问用户是否区分大小写
bDiv = IsDiv();
//替换文件内容
FileWordReplace( szFileName, &wordOld, &wordNew, bDiv );
return 0;
}
//从文件中读取一行句子
int ReadSentence( FILE *pFile, PSENTENCE pSentence )
{
char szFormat[20] = {0};
szFormat[0] = '%';
sprintf( szFormat + 1, "%d[^\n]", BUFFER_SIZE / 4 - 1 );
memset( pSentence->szValue, 0, BUFFER_SIZE );
fscanf( pFile, szFormat, pSentence->szValue );
pSentence->nLength = (int)strlen( pSentence->szValue );
return fgetc( pFile ) * !pSentence->nLength;
}
//将一条句子写进文件中
int WriteSentence( FILE *pFile, PSENTENCE pSentence )
{
int nWrited;
nWrited = (int)fwrite( pSentence->szValue, sizeof(char), pSentence->nLength, pFile );
fputc( '\n', pFile );
return nWrited + 1;
}
//判断一个字母是否是小写
BOOL IsLower( char c )
{
return c >= 'a' && c <= 'z' ;
}
//将一个字符转化为大写
char ToUper( char c )
{
return c - 'a' + 'A';
}
//用一条一般句子生成一条不含小写字母的句子
char* MakeUper( const char *szSource, const int nLength )
{
char *szTemp = (char*)malloc( sizeof(char) * BUFFER_SIZE );
int i;
for( i=0; i<nLength; i++)
szTemp[i] = IsLower( szSource[i] ) ? ToUper( szSource[i] ) : szSource[i];
szTemp[i] = 0;
return szTemp;
}
//将句子中的单词(短语)替换成新单词, 区分大小写
int StringWordReplace( PSENTENCE pSentence, PWORD pWordOld, PWORD pWordNew )
{
char *szTemp = (char*)malloc( sizeof(char) * BUFFER_SIZE );
int nPos = 0, nTimes = 0;
const int nDif = pWordNew->nLength - pWordOld->nLength;
while( nPos <= pSentence->nLength - pWordOld->nLength )
{
if( !strncmp( pSentence->szValue + nPos, pWordOld->szValue, pWordOld->nLength ) )
{
strcpy( szTemp + nPos + nTimes * nDif, pWordNew->szValue );
nPos += pWordOld->nLength;
nTimes++;
}
else
{
szTemp[nPos + nTimes * nDif] = pSentence->szValue[nPos];
nPos++;
}
}
strcpy( szTemp + nPos + nTimes * nDif, pSentence->szValue + nPos );
pSentence->nLength += nDif * nTimes;
free( pSentence->szValue );
pSentence->szValue = szTemp;
return nTimes;
}
//将句子中的单词(短语)替换成新单词, 不区分大小写
int StringWordReplaceEx( PSENTENCE pSentence, PWORD pWordOld, PWORD pWordNew )
{
char *szTemp = (char*)malloc( sizeof(char) * BUFFER_SIZE );
char *szUperSentence = MakeUper( pSentence->szValue, pSentence->nLength );
char *szUperWord = MakeUper( pWordOld->szValue, pWordOld->nLength );
int nPos = 0, nTimes = 0;
const int nDif = pWordNew->nLength - pWordOld->nLength;
while( nPos <= pSentence->nLength - pWordOld->nLength )
{
if( !strncmp( szUperSentence + nPos, szUperWord, pWordOld->nLength ) )
{
strcpy( szTemp + nPos + nTimes * nDif, pWordNew->szValue );
nPos += pWordOld->nLength;
nTimes++;
}
else
{
szTemp[nPos + nTimes * nDif] = pSentence->szValue[nPos];
nPos++;
}
}
strcpy( szTemp + nPos + nTimes * nDif, pSentence->szValue + nPos );
free( szUperSentence );
free( szUperWord );
pSentence->nLength += nDif * nTimes;
free( pSentence->szValue );
pSentence->szValue = szTemp;
return nTimes;
}
//从用户那里获取一个单词
void GetWord( PWORD pWord )
{
memset( pWord->szValue, 0, 256 );
puts("输入单词(可以是中间有空格的短语), 但不能超过255个字符");
scanf("%255[^\n]", pWord->szValue );
fflush( stdin );
pWord->nLength = strlen( pWord->szValue );
}
//将旧文件删除, 将新文件改成旧文件的名字
//因为文件中单词替换后文件长度可能改变, 所以只能生成新文件再删除旧的
void DelAndReNameFile( const char *szFileName, const char *szTempFileName )
{
char szCmd[128] = {0};
const char *pCTemp;
strcpy( szCmd, "del ");
strcpy( szCmd + 4, szFileName );
system( szCmd );
memset( szCmd, 0, 128 );
strcpy( szCmd, "ren ");
strcpy( szCmd + 4, szTempFileName );
szCmd[strlen(szCmd)] = ' ';
pCTemp = strrchr( szFileName, '\\' );
strcpy( szCmd + strlen(szCmd), pCTemp ? (pCTemp + 1) : szFileName );
system( szCmd );
}
//在旧文件相同的路径下给新文件取名, 文件名是某单词的md5码
void GetTempFileName( const char *szFileName, char *szTempFileName )
{
const char *pCTemp;
pCTemp = strrchr( szFileName, '\\' );
if( pCTemp )
strncpy( szTempFileName, szFileName, pCTemp - szFileName + 1 );
strcpy( szTempFileName + strlen(szTempFileName), "22EFDBE132EABC102306BD7A334FB434" );
}
//以下几个函数的说明在声明处
void FileWordReplace( const char *szFileName, PWORD pWordOld, PWORD pWordNew, BOOL bDiv )
{
FILE *pFileS, *pFileD;
char szTempFileName[128] = {0};
SENTENCE sentence;
GetTempFileName( szFileName, szTempFileName );
sentence.szValue = (char*)malloc( sizeof(char) * BUFFER_SIZE );
pFileS = fopen( szFileName, "r" );
pFileD = fopen( szTempFileName, "w" );
while( EOF != ReadSentence( pFileS, &sentence ) )
{
if( bDiv )
StringWordReplace( &sentence, pWordOld, pWordNew );
else
StringWordReplaceEx( &sentence, pWordOld, pWordNew );
WriteSentence( pFileD, &sentence );
}
fclose( pFileS );
fclose( pFileD );
free( sentence.szValue );
DelAndReNameFile( szFileName, szTempFileName );
}
void GetPairWord( PWORD pWordOld, PWORD pWordNew )
{
puts("输入要替换的单词 : ");
GetWord( pWordOld );
puts("输入新的单词 : ");
GetWord( pWordNew );
}
BOOL IsDiv()
{
char cInput;
while( TRUE )
{
puts("区分大小写吗? (Y/N)");
cInput = getchar();
if( 'y' == cInput || 'Y' == cInput )
return TRUE;
else if( 'n' == cInput || 'N' == cInput )
return FALSE;
}
}
void GetFileName( char *szFileName )
{
FILE *pFile;
szFileName[63] = 0;
while( TRUE )
{
puts("输入文件名");
if ( 1 == scanf( "%63s", szFileName ) )
{
fflush( stdin );
if( pFile = fopen( szFileName, "r" ) )
{
fclose( pFile );
break;
}
else
puts("找不到文件");
}
}
}
最后一种方法, 去除了校验等...
#include <stdio.h>
#include <string.h>
// 缓冲区, 用来放读取的内容
#define BUFFER_SIZE 1024
// 部分大小写的字符串比较
int MyCmp( const char *sz1, const char *sz2, int nLen )
{
char cMax, cMin;
for( ;nLen--; sz1++, sz2++ )
{
if( *sz1 == *sz2 )
continue;
cMax = *sz1 >= *sz2 ? *sz1 : *sz2;
cMin = *sz1 + *sz2 - cMax;
if( (cMax <= 'z' && cMax >= 'a') && (32 == cMax - cMin) )
continue;
return *sz1 - *sz2;
}
return 0;
}
int main()
{
// 文件指针
FILE *pFileS, *pFileD;
// 输入缓冲; 要替换的单词; 新的单词
char buffer[BUFFER_SIZE], wordOld[64] = {0}, wordNew[64] = {0};
// 缓冲区实际大小; 缓冲区操作标志; 每次读取的字节数; 要替换的单词长度; 新单词的长度; 区分大小写标志
int nTop = 0, nPoint, nRead, nOldLen, nNewLen, bDiv;
puts("输入两个词");
scanf("%63s%63s", wordOld, wordNew);
nOldLen = strlen(wordOld), nNewLen = strlen(wordNew);
puts("是否区分大小写? 输入1(Yes),0(No)");
scanf("%d", &bDiv);
pFileS = fopen("a.txt", "rb");
pFileD = fopen("aa.txt", "wb");
while( nRead = fread( buffer + nTop, sizeof(char), BUFFER_SIZE - nTop, pFileS) )
{
nTop += nRead;
// 处理旧单词整数倍的那部分缓冲
for( nPoint=0; nPoint<=nTop - nOldLen; )
{
// 如果要区分则用memcmp比较, 不区分用自定义比较函数比较
if( !( bDiv ? memcmp( buffer + nPoint, wordOld, nOldLen) : MyCmp( buffer + nPoint, wordOld, nOldLen )) )
{
// 相等, 写进文件
fwrite( wordNew, sizeof(char), nNewLen, pFileD );
// 缓冲区标志(相当于指针)移位
nPoint += nOldLen;
}
else
// 不相等, 将该字节写入并移动1字节
fputc( buffer[nPoint++], pFileD );
}
// 处理剩下的缓冲区; 方法同上
if( !( bDiv ? memcmp( buffer + nPoint, wordOld, nTop - nPoint ) : MyCmp( buffer + nPoint, wordOld, nTop - nPoint)) )
{
memcpy( buffer, buffer + nPoint, nTop - nPoint );
nTop = nTop - nPoint;
}
else
{
fwrite( buffer + nPoint, sizeof(char), nTop - nPoint, pFileD );
nTop = 0;
}
}
// 关闭文件
puts("转化成功");
fclose( pFileS ), fclose( pFileD );
return 0;
}
温馨提示:答案为网友推荐,仅供参考