• 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'。';
    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# 正则 获取 Img Src路径
    .NET动态加载用户控件并传值的方法
    ViewState压缩技术
    BookStrap中关于button和图片的注意点
    在idea中使用Git
    了解Git的使用
    javascript-----DOM文档对象模型
    浅谈java集合
    javaI/O流
    二进制和十进制的转换
  • 原文地址:https://www.cnblogs.com/zjutzz/p/3207894.html
Copyright © 2020-2023  润新知