博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vc6.0:中文字串的读取
阅读量:7009 次
发布时间:2019-06-28

本文共 2434 字,大约阅读时间需要 8 分钟。

#include#include#include#includeusing namespace std;int main(){locale china("chs");wcin.imbue(china);                            //use locale objectwcout.imbue(china);wstring title;wchar_t wc = L'。';while(getline(wcin, title, wc)){size_t len = title.length();                //size_t可以换成intsize_t i, j;for(i=0; ifor(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#includeusing namespace std;int main(){string title;int i, j;while(cin >> title){int len = title.length();for(i=0; ifor(j=i+1; j<=len; j++){string keyword = title.substr(i, j-i);cout << "keyword=\'" << keyword << "\'" << endl;}}}return 0;}

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


详细的sample解释程序,可以看这里:http://stackoverflow.com/questions/402283/stdwstring-vs-stdstring
以及这里:http://www.cnblogs.com/xiaoyz/archive/2008/10/11/1308860.html

 

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

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


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



你可能感兴趣的文章
HDU 3613 Best Reward 正反两次扩展KMP
查看>>
[AFUI]App Framework
查看>>
view类的setVisibility
查看>>
zepto.js 源码解析
查看>>
HTTP状态码大全
查看>>
使用ASP.NET Web API 2创建OData v4 终结点
查看>>
MyBatis简单的增删改查以及简单的分页查询实现
查看>>
Android快捷支付SDK Demo resultStatus={4001};memo={參数错误};result={}问题
查看>>
urllib2中自定义opener
查看>>
Hadoop快速入门
查看>>
MySql_安装及简单命令
查看>>
CSDN markdown 编辑器 第四篇 LaTex语法
查看>>
Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
查看>>
ubuntu14安装redis
查看>>
DICOM:C-GET与C-MOVE对照剖析
查看>>
什么是跨域请求
查看>>
利用__index和__newindex实现默认值表、监控表、只读表(转)
查看>>
HDU 1247 Hat’s Words (字典树 &amp;&amp; map)
查看>>
spring boot启动原理步骤分析
查看>>
用swift开发仪表盘控件(二)
查看>>