Window C++ 读写 UTF16-LE 文件

file.h

#include<string>
#include<Windows.h>
#include <atlstr.h>
#include <atltrace.h>
#include <fstream>
#include <vector>
using namespace std;
class FileHelper {
private:
	bool first = true;
	wchar_t str[1024] = { 0 };
	void UnicodeToANSI(wchar_t* src, char* dst);
	/// <summary>
	/// 
	/// </summary>
	/// <param name="out"></param>
	void writeUnicodeFileHead(std::ofstream& out);
	/// <summary>
	/// 
	/// </summary>
	/// <param name="out"></param>
	/// <param name="str"></param>
	/// <param name="size"></param>
	void writeUnicodeFileContent(std::ofstream& out, wchar_t const* str, int size);
	/// <summary>
	/// 
	/// </summary>
	/// <param name="out"></param>
	void writeUnicodeFileCRLF(std::ofstream& out);
public:
	string getEncoding(string path);
	void writeUnicodeLine(std::ofstream& out, CString& str);
	void readUnicodeLine(std::wstring& filename,std::vector<string> & lines);
};

file.cpp

#include "file.h"
void FileHelper::UnicodeToANSI(wchar_t* src, char* dst) {
	// wcslen 获取 src 长度
	//  wcslen(src) << 1 扩大2的1次方
	WideCharToMultiByte(CP_ACP, NULL, src, -1, dst, wcslen(src) << 1, NULL, FALSE);
}
void FileHelper::readUnicodeLine(std::wstring& filename, std::vector<string>& lines) 
{
	FILE* fp = NULL;
	int err = _wfopen_s(&fp, filename.c_str(), L"r, ccs=UTF-16LE");
	if (!err && fp != NULL) {
		while (fgetws(str, 1024, fp) != NULL)
		{
			char result[1024] = { 0 };
			UnicodeToANSI(str, result);
			string data = result;
			lines.push_back(data);
		}
	}
}

void FileHelper::writeUnicodeFileHead(std::ofstream& out)// 写入文件内容前,先写入BOM
{
	char const* const utf16head = "\xFF\xFE";
	out.write(utf16head, 2);
}
void FileHelper::writeUnicodeFileContent(std::ofstream& out, wchar_t const* str, int size)
{
	char const* pos = (char const*)str;
	out.write(pos, size);
}
void FileHelper::writeUnicodeFileCRLF(std::ofstream& out)// 写入回车换行符
{
	char const* const utf16head = "\x0D\x00\x0A\x00";
	out.write(utf16head, 4);
}
void FileHelper::writeUnicodeLine(std::ofstream& out, CString& str)
{
	if (first) {
		writeUnicodeFileHead(out);
		first = false;
	}
	writeUnicodeFileContent(out, str, str.GetLength() * 2);
	writeUnicodeFileCRLF(out);
}

string FileHelper::getEncoding(string path) {
	ifstream fin(path, ios::binary);
	unsigned char  s2;
	fin.read((char*)&s2, sizeof(s2));//读取第一个字节,然后左移8位
	int p = s2 << 8;
	fin.read((char*)&s2, sizeof(s2));//读取第二个字节
	p += s2;
	string code;
	switch (p)//判断文本前两个字节
	{
	case 0xfffe:  //65534
		code = "Unicode";
		break;
	case 0xfeff://65279
		code = "Unicode big endian";
		break;
	case 58768://59042
		code = "UTF-8";
		break;
	case 50171:
		code = "ANSI";
		break;
	}
	fin.close();
	return code;
}

main.cpp

#include "file.h"
#include <iostream>
#pragma warning(disable:4996)
using namespace std;

int main(void)
{
	ofstream out("test.csv", std::ios::binary | std::ios::out);
	FileHelper* fileHelper = new FileHelper();
	CString str("张三\t李四\t王五");
	fileHelper->writeUnicodeLine(out, str);
	out.close();

	wstring filename = L"test.csv";
	vector<string> lines;

	fileHelper->readUnicodeLine(filename, lines);

	for (int i = 0; i < lines.size(); i++) {
		std::cout << lines[i] << endl;
	}
	system("pause");
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波塞冬的祝福

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值