65,186
社区成员




#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//subs1是要查找的字符串
//subs2 是要将subs1从text中替换掉的字符串
void replace(wstring &text, const wstring &subs1, const wstring &subs2)
{
wcout<<subs1<<" "<<subs1.size()<<endl;
wcout<<subs2<<" "<<subs2.size()<<endl;
wstring::size_type beg = text.find(subs1);
cout<<beg<<endl;
if(beg >= text.size())
{
cout<<"不能匹配"<<endl;
return;
}
text.erase(beg, subs1.size());
text.insert(beg, subs2);
/*while(beg < text.size())
{
beg = text.find(subs1);
text.erase(beg, subs1.size());
text.insert(beg, subs2);
}*/
}
//从file中读取文本,然后将str给初始化。
void init(wifstream &is, const string &file, wstring &str)
{
is.close();
is.clear();
is.open(file.c_str());
str = L"";
if(!is)
{
cerr<<"Cannot open the file : "<<file<<endl;
return;
}
wstring line;
locale china("chs");
wcout.imbue(china);
while(getline(is,line))
{
str = str + line + L"\n";
wcout << str << endl;
}
//string s = str.rfind("\n");
str.erase(str.rfind(L"\n"), str.size());
}
//将替换好的text再重新输出到一个文件中
void output_file(wofstream &os, const string &out_file, const wstring &str)
{
os.close();
os.clear();
os.open(out_file.c_str());
if(!os)
{
cerr<<"Cannot output the string to the file : "<<out_file<<endl;
}
os<<str<<endl;
os.close();
}
int main()
{
//file path F:\\sb\\sb3\\www.bill-jc.com\\test.html
string in_file = "F:\\test3.txt";
string out_file = "f:\\test.html";
string str1_file = "f:\\test1.txt";
string str2_file = "f:\\test2.txt";
//variable
locale china("chs");
wstring text, str1, str2;
wifstream is;
wofstream os;
is.imbue(china);
os.imbue(china);
init(is, in_file, text);
while(true)
{
init(is, str1_file, str1);
init(is, str2_file, str2);
replace(text, str1, str2);
cout<<"Y/N"<<endl;
char i;
cin>>i;
if(i == 'N')
break;
}
//output_file(os, out_file, text);
return 0;
}
把文本文件存成UTF-8。然后用下面的方法读取。 http://blog.sina.com.cn/s/blog_6b88065a01015u58.html
请认真阅读我的回帖中的每个建议和链接。
[quote=引用 8 楼 vipcxj 的回复:] 首先保证你的文件是以utf-16或ucs-2储存的,然后用wfstream读取,若要在标准输入输出设备上显示正常,还得 setlocale(LOCALE_ALL, ""); //""就是本地默认设置,LZ是中国人吧,用的中国产电脑,本地默认设置肯定就能认出中文汉字。 至于utf-8,windows上貌似不支持,必须要手动转码。当然如果LZ知道utf-8的储存格式,也不是很难,可以一字节一字节判断,然后自己转成windows支持的wchar_t的unicode,然后再显示或者查找。 如果不是unicode的,那么对于windows上的多字节编码,汉字占2字节,查找时也要按2字节查找。
[quote=引用 15 楼 zhao4zhong1 的回复:] 啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A 楼主试试随便用哪个号称能识别汉字的编辑器打开这个文件: 内容为以下9个字节: 0xB0 0xA1 0x4A 0x55 0x55 0x4A 0xE5 0x95 0x8A
#include <stdio.h>
char b[]={0xB0,0xA1,0x4A,0x55,0x55,0x4A,0xE5,0x95,0x8A};
FILE *f;
int main() {
f=fopen("d:\\9bytes.txt","wb");
fwrite(b,1,9,f);
fclose(f);
return 0;
}
[quote=引用 10 楼 Jim_King_2000 的回复:] 把文本文件存成UTF-8。然后用下面的方法读取。 http://blog.sina.com.cn/s/blog_6b88065a01015u58.html
控制台?我不熟悉linux,我就试着把这个程序给编辑了下,没有设置linux。 你看我下面的想法对吗? 1 C++中读取中文文本时的问题。 2 文本的编码方式与程序读取文本中的字符是什么关系? 3 当程序从文件中读取 字符时,原理是什么? 4 一个文本是怎样编码的? 5 UTF-8 ,Unicode,几种编码方式的关系。 6 编码是什么? 7 以什么样的编码存储? 8 文件存储到电脑上的方式,
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream fs("hello.txt", ios_base::trunc | ios_base::out);
fs << "你好" << endl;
fs.close();
fs.open("hello.txt", ios_base::in);
string str;
fs >> str;
cout << str << endl;
fs.close();
return 0;
}
如果不能正常显示,你在看看你所说的控制台(我不确定你说的控制台是什么)有没有“设置或settings”的选项,将编码设为UTF-8。
如果还不行,搜一下你的编译器有没有设置语言编码的选项。这个用法是在linux上的?我在linux上编译能够正常使用,但是在控制台上输出 wcout<<subs1<<" "<<subs1.size()<<endl; 这个输出中文的时候,总是乱码。不过中文文本字符替换能正常进行。
windows上我一般用_wsetlocale(),中文一切正常,linux下没试过,感觉是不是还和发行版有关系?
In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this: fopen("newfile.txt", "rw, ccs=<encoding>"); Allowed values of the encoding include UNICODE, UTF-8, and UTF16-LE. If the file is already in existence and is opened for reading or appending, the Byte Order Mark (BOM) is used to determine the correct encoding. It is not necessary to specify the encoding with a flag. In fact, the flag will be ignored if it conflicts with the type of the file as indicated by the BOM. The flag is only used when no BOM is present or if the file is a new file. The following table summarizes the modes used in for various flags given to fopen and Byte Order Marks used in the file. Encodings Used Based on Flag and BOM Flag No BOM (or new file) BOM: UTF-8 BOM: UTF-16 UNICODE ANSI UTF-8 UTF-16LE UTF-8 UTF-8 UTF-8 UTF-16LE UTF-16LE UTF-16LE UTF-8 UTF-16LE