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");
}