vc6.0:中文字串的读取

简介: #include #include #include #include using namespace std; int main(){ locale china("chs"); wcin.imbue(china); //use locale object wcout.imbue(china); wstring title; wchar_t wc = L'。
#include
#include
#include
#include
using namespace std;

int main(){
locale china("chs");
wcin.imbue(china);                            //use locale object
wcout.imbue(china);
wstring title;
wchar_t wc = L'。';
while(getline(wcin, title, wc)){
size_t len = title.length();                //size_t可以换成int
size_t i, j;
for(i=0; i
for(j=i+1; j<=len; j++){
wstring keyword = title.substr(i, j-i);
// cout << "keyword=\'" << keyword << "\'" << endl;
wcout << keyword << endl;
}
}
}
}

在vc6中,一个中文字符占两个字节,当给定一个中文字符串时,如何输出其所有子串?
1.其循环算法不是大问题,可以这样写:

#include
#include
#include
#include
using namespace std;

int main(){
string title;
int i, j;
while(cin >> title){
int len = title.length();
for(i=0; i
for(j=i+1; j<=len; j++){
string keyword = title.substr(i, j-i);
cout << "keyword=\'" << keyword << "\'" << endl;
}
}
}
return 0;
}



但是要处理的是中文字符,如果每次考虑读取两个字符,那么一旦是中英文混合输入,就有一些子串取不到,而且中文子串只输出一个char的时候是乱码,每个中文字对应的连续两个char连续输出时才不是乱码。
解决方法是用wstring。sample程序如下:




 

在做LMS的时候需要处理一些从文件读入的数据:书名、作者、出版社等,当中含有中文字段,需要用wstring处理。
现在,图书或读者信息存储在txt文件中,如何操作?

在sjtu的一个页面上发现了一个解决方案:
  • 传统的string只能应用于有限的西文字符,由于图书馆的信息中包含中文字符,所以我们需要引入wstring。
  • 我们从外部文件读入数据,采用原始的string读入,然后再相应转换为wstring。
  • 相应的,当我们将数据写入外部文件时,先将wstring转换为string,然后写入。
  • 对于Windows用户,请在程序头include windows.h。
我认为上面有小错误,做了修改,得到两个正确的函数。为了说明问题,这里举一个sample程序:


#include
#include
#include
#include
#include
#include
#include
using namespace std;

inline string wtos(const wstring&w)
{
    int len= WideCharToMultiByte(GetACP(),0,w.c_str(),-1,NULL,0,NULL,NULL);
    char *buf= new char[len];
    WideCharToMultiByte(GetACP(),0,w.c_str(),-1,buf,len,NULL,NULL);
    string s(buf);
    delete[] buf;
    return s;
}

inline wstring stow(const string &s)
{
    int len= MultiByteToWideChar(GetACP(),0,s.c_str(),-1,NULL,0);
    wchar_t*buf= new wchar_t[len];
    MultiByteToWideChar(GetACP(),0,s.c_str(),-1,buf,len);
    wstring w(buf);
    delete[] buf;
    return w;
}

int main(){
locale china("chs");
wcin.imbue(china);//use locale object
wcout.imbue(china);
ifstream fin("chris.txt");
string title;
wstring ret;
while(getline(fin, title)){
istringstream sin(title);
wstring ret;
while(sin >> title){
ret = stow(title);
wcout << L"ret = " << ret << endl;
}
}
}




chirs.txt文件的内容:
算 法   导论abc

输出截屏:

sample中,从文件中读入整行的string,然后用istringstream的方法读入每一个小的string,读入后再用stow()转化之,这样就可以使用了!!



目录
相关文章
|
6月前
|
IDE 开发工具 C++
[记录][问题]Win32调用C++/WinRT DLL
[记录][问题]Win32调用C++/WinRT DLL
|
3月前
|
IDE API 开发工具
visual studio 生成dll文件以及修改输出dll文件名称操作
visual studio 生成dll文件以及修改输出dll文件名称操作
80 0
VC6和VS2005(VC8)各项目默认运行时库
VC6和VS2005(VC8)各项目默认运行时库
|
C++ 索引
VC通过函数索引调用DLL范例
VC通过函数索引调用DLL范例
48 0
vc6.0不能正常打开文件
vc6.0不能正常打开文件
128 0
vc6.0不能正常打开文件
|
C#
win10 uwp 读取保存WriteableBitmap 、BitmapImage
原文:win10 uwp 读取保存WriteableBitmap 、BitmapImage 我们在UWP,经常使用的图片,数据结构就是 BitmapImage 和 WriteableBitmap。
1845 0