首页 > 编程语言 > 详细

c++ 读取TXT文件,中文乱码处理

时间:2020-07-28 15:15:02      阅读:410      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <windows.h>
 
using namespace std;
 
 
string UTF8ToGB(const char* str)
{
    string result;
    WCHAR *strSrc;
    LPSTR szRes;
 
    //获得临时变量的大小
    int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
    strSrc = new WCHAR[i + 1];
    MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);
 
    //获得临时变量的大小
    i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
    szRes = new CHAR[i + 1];
    WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
 
    result = szRes;
    delete[]strSrc;
    delete[]szRes;
 
    return result;
}
 
int main(int argc, char *argv[])
{
    ifstream in;
    in.open("C:/in.txt");
 
    string s;
    ofstream out;
    out.open("c:/output.txt");
    //getline(t, s);
    //out << s << "\n";
 
    while (std::getline(in, s))
    {
        string str = UTF8ToGB(s.c_str()).c_str();
        //对每一行进行操作。
        out << str << "\n";
 
    }
    out.close();
    in.close();
 
 
    system("pause");
    return 0;
}

 

c++ 读取TXT文件,中文乱码处理

原文:https://www.cnblogs.com/zhaopengpeng/p/13390535.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!