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



目录
相关文章
|
编译器 C语言
C语言预处理详解
C语言预处理详解
|
8月前
|
机器学习/深度学习 人工智能 自然语言处理
《深度揭秘:DeepSeek如何解锁自然语言处理密码》
DeepSeek是基于Transformer架构的自然语言处理(NLP)佼佼者,通过自注意力机制高效捕捉长距离依赖关系,优化语义理解和生成。预训练阶段,DeepSeek利用海量文本数据学习语法、语义等知识,确保多义词的准确理解与翻译。监督微调和强化学习从人类反馈进一步提升模型性能,使其在智能客服、写作辅助、信息检索等领域广泛应用,为AI语言应用开辟新道路。
275 2
|
12月前
|
存储 NoSQL Java
Jedis客户端介绍
【10月更文挑战第12天】
pywinauto教程
pywinauto教程
464 7
|
机器学习/深度学习 数据可视化 TensorFlow
使用Python实现深度学习模型:智能旅游路线规划
使用Python实现深度学习模型:智能旅游路线规划
281 2
|
分布式计算 Spark 大数据
Apache Spark中国技术交流社区历次直播回顾(持续更新)
Apache Spark中国技术交流社区,由阿里巴巴开源大数据技术团队成立,持续输出spark相关技术直播、原创文章、精品翻译,钉钉群内千人交流学习,欢迎加入。钉钉入群 https://qr.dingtalk.com/action/joingroup?code=v1,k1,jmHATP9Tk+okK7QZ5sw2oWSNLhkt2lCRvfHRdW7XhUQ=&_dt_no_comment=1&origin=11 更多视频和ppt资料请入群获得。
Apache Spark中国技术交流社区历次直播回顾(持续更新)
|
SQL 数据可视化
数据可视化平台Datart-创建SQL视图
数据可视化平台、Datart
350 0
|
机器学习/深度学习 人工智能 安全
Human Generator:创建人体模型的 AI 工具
Human Generator:创建人体模型的 AI 工具
474 0
|
边缘计算 开发工具 UED
互联网新半场的流量生态——阿里云定向免流方案
当你正在游戏中肆意厮杀时,在你跟美女主播互动畅聊时,突然收到这样一条短信,你会不会心头一紧呢?80%的调查反馈显示,是的(个别土豪除外)。毕竟5块钱30M流量就能用一个月的时代已经过去了,我们玩的游戏也从XX玛丽、贪吃X等单机弱联网游戏过渡到了吃量狂魔的MMO、MOBA等大型网游之中。
8894 0