fstream類讀取UTF-八、Unicode和ANSI文本文檔亂碼問題的解決方案

一、解決UTF-8類型的文本文檔中文亂碼讀取(思路:將UTF-8轉成Unicode而後再轉ANSI)ios

#include <fstream>
#include <iostream>
#include <string>
// #include <afx.h>
#include <Windows.h>app

//changeTextFromUtf8ToAnsi讀取UTF-8格式的文件並將之保存爲ANSI格式的文件ide

void changeTextFromUtf8ToAnsi(const char* filename)
{
ifstream infile;string strLine="";string strResult="";
infile.open(filename);
if (infile)
{
   while(!infile.eof()){
    getline(infile,strLine);
    strResult+=strLine+"\n";
   }
}
infile.close();
char* changeTemp=new char[strResult.length()];
strcpy(changeTemp,strResult.c_str());
char* changeResult=changeTxtEncoding(changeTemp);
strResult=changeResult;函數

ofstream outfile;
outfile.open("I:\\ANSI.txt");
outfile.write(strResult.c_str(),strResult.length());
outfile.flush();
outfile.close();
}測試

//changeTxtEncoding修改字符串的編碼編碼

char* changeTxtEncoding(char* szU8){
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
wchar_t* wszString = new wchar_t[wcsLen + 1];
::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen);
wszString[wcsLen] = '\0';
cout<<wszString<<endl;spa


int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
char* szAnsi = new char[ansiLen + 1];
::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL);
szAnsi[ansiLen] = '\0';
return szAnsi;
}code

二、解決Unicode類型的文本文檔中文亂碼讀取(此方法經測試不可用於打開ANSI的文本文檔)文檔

string ws2s(const std::wstring& ws)和string readTxt(char* filename)函數轉帖自CSDN,readTxt函數由本人進行了一個小小的bug修正,從而能夠避免末字符重複出現的問題;字符串

// fstream中文亂碼解決方案.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
string ws2s(const std::wstring& ws);
string readTxt(char* filename);
int _tmain(int argc, _TCHAR* argv[])
{
readTxt("E:\\testUnicode.txt");
}


std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
std::string result = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}

std::string readTxt(char* filename) {
ifstream fin;
fin.open(filename, ios::binary);
size_t index = 2;
std::string strRet;
std::string strLineAnsi;
std::wstring wstrLine;

while (!fin.eof())
{
   fin.seekg(index, ios::beg);
   wchar_t wch;
   fin.read((char *)(&wch), 2);
   if (wch == 0x000D) // 判斷回車
   {
    strLineAnsi = ws2s(wstrLine);
    wstrLine.erase(0, wstrLine.size() + 1);
    index += 2; // 跳過回車符和行開頭符
    strRet = strRet + strLineAnsi;
   }
   else
   {
    wstrLine.append(1, wch);
    index += 2;
   }
}
strLineAnsi = ws2s(wstrLine);
strRet = strRet + strLineAnsi;
fin.close();
//if語句解決文本段末有回車致使最後字符集重複的問題
if (strLineAnsi!="")
{
   strRet=strRet.substr(0,strRet.length()-1);
}
printf("%s", strRet.c_str());
return strRet;
}

三、解決ANSI類型的文本文檔中文亂碼讀取(此方法經測試不可用於打開Unicode的文本文檔)

#include "stdafx.h" #include <fstream> #include <iostream> #include <string> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { ifstream infile; string strResult=""; string strTemp=""; infile.open("E:\\testANSI.txt"); if (infile) {    while(!infile.eof())    {         getline(infile,strTemp);      strResult+=strTemp;    } } infile.close(); cout<<strResult; }

相關文章
相關標籤/搜索