开发者社区> lovedan> 正文

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()转化之,这样就可以使用了!!



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
VC通过函数索引调用DLL范例
VC通过函数索引调用DLL范例
20 0
vc读INI文件
vc读INI文件
17 0
PE格式:新建节并插入DLL
PE格式是 Windows下最常用的可执行文件格式,理解PE文件格式不仅可以了解操作系统的加载流程,还可以更好的理解操作系统对进程和内存相关的管理知识,而有些技术必须建立在了解PE文件格式的基础上,如文件加密与解密,病毒分析,外挂技术等。
33 0
Qt实用技巧:win将ffmpeg、opengl、osg等各种库封装成qt模块,运行需要dll,增加自动拷贝运行库到exe目录执行脚本
Qt实用技巧:win将ffmpeg、opengl、osg等各种库封装成qt模块,运行需要dll,增加自动拷贝运行库到exe目录执行脚本
108 0
win10 uwp 读取保存WriteableBitmap 、BitmapImage
原文:win10 uwp 读取保存WriteableBitmap 、BitmapImage 我们在UWP,经常使用的图片,数据结构就是 BitmapImage 和 WriteableBitmap。
1716 0
+关注
lovedan
计算机科学与技术硕士,专注计算机视觉(目标检测、深度学习),关注Linux环境下各算法配置。
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载