划分中文字符串,宽字符的插入

需求:
将一串中文字符以某个关键字插入某个字符从而划分成多个串;
环境
Microsoft Visual Studio 2015; Windows 10;

对c++处理宽字符不是很懂,百度了一堆才刚刚完成需求。

#include <iostream>
#include <string>
#include <locale>
#include <fstream>
using namespace std;
/*
如果要用wcout,需要在使用之前按语句
    wcout.imbue(locale(locale(),"",LC_CTYPE));将其locale设置为本地语言; 
如果要用ofstream或wofstream,要在打开文件之前按语句
    locale &loc=locale::global(locale(locale(),"",LC_CTYPE)); 
    将全局locale设为本地语言并保存初始的全局locale。
    然后在打开文件之后,按语句  locale::global(loc); 将全局locale恢复为初始值; 
*/

int main() {
    locale &loc = locale::global(locale(locale(), "", LC_CTYPE)); 

    //打开文件
    wifstream fin;
    wofstream fout("C:/111/0.csv");
    string filename;
    string buf_str;
    buf_str = "1";
    filename = "C:/111/" + buf_str + ".csv";
    fin.open(filename.c_str());
    if (!fin) {
        cerr << "open the file " << filename << " error" << endl;
        exit(1);
    }

    locale::global(loc); 

    //操作
    wstring str;
    while (fin >> str) {
        wcout.imbue(locale(locale(), "", LC_CTYPE));//要使用wcout 先写这句
        for (size_t i = 0; i < str.size();i++){
            wcout << str[i]; 
            wchar_t c = L'处';
            if (str[i]== c) {
                wstring::iterator iter = str.begin() + i+1;
                str.insert(iter, ',');
                //str.insert(iter, L'会');插入宽字符
                i++;
            }
        }
        cout << endl;
        fout << str << endl;
    }
    fin.close();
    fout.close();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值