- 论坛徽章:
- 0
|
#include<iostream>
#include<fstream>
#include<string>
#include<map>
#include<locale>
using namespace std;
wstring* replace_str(wstring &str_str,wchar_t c_wchr,wchar_t nc_wchr);
std::wstring s2ws(const std::string& s);
std::string ws2s(const wstring& ws);
int main(int argc, char *argv[])
{
/*设置区域化输出*/
//setlocale(LC_ALL, "chinese-simplified");
locale loc( "chs" );
//locale loc( "Chinese-simplified" );
//locale loc( "ZHI" );
//locale loc( ".936" );
wcout.imbue(loc);
/*设置区域化输出*/
...
...
...
...
ifstream inFile;
if (argc == 1) {
cerr << "请输入正确的参数!" << endl;
return EXIT_FAILURE;
}
inFile.open(argv[1]);
if (!inFile) {
cerr << "无法打开文件:" << endl;
return EXIT_FAILURE;
}
wstring content;
string line, tmpws;
while(getline(inFile, line)) {
tmpws = tmpws + line + "\n";
}
cout << tmpws << endl;
content = s2ws(tmpws);
for(map<wchar_t, wchar_t>::const_iterator iter = mps.begin(); iter != mps.end(); ++iter)
{
replace_str(content, iter->first, iter->second);
}
wcout << content << endl;
inFile.close();
return 0;
}
wstring* replace_str(wstring &str_str,wchar_t c_wchr,wchar_t nc_wchr)
{
for(wstring::iterator iter = str_str.begin(); iter != str_str.end(); ++iter)
if (*iter == c_wchr)
*iter = nc_wchr;
return &str_str;
}
string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
std::string result = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
wstring s2ws(const std::string& s)
{
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring result = _Dest;
delete []_Dest;
setlocale(LC_ALL, "C");
return result;
}
|
任务:从文件中读入中英混排的文本文件,要把其中部份字更换成其他字。
刚开始使用的是普通的string及char实现替换,但是因为替换时会出现二次替换和多次替换的情况而出现乱码,因此想通过把字符转为wchar_t类型,string转换为wstring再操作。
刚开始测试时使用的是纯汉字,没有换行,效果很好,但是当string中有字母和标点符号及换行符时,转变后的wstring就会从出现字母或标点及换行符处开始变成乱码。
转换函数是从网上找的,这种情况怎么解决呢????难道string转到wstring只限于汉字吗????
谢谢! |
|