返回信息流在使用ReadString读取文件内容的时候,没有按照unicode编码的双字节来读,而是按照单字节读,然后按照双字节填充。比如文件中是0x0041, 0x0042,读出来就变成了0x0041,0x0000,0x0042,0x0000这样子,有人知道怎么能让ReadString直接按双字节读取填充么?程序环境也是Unicode的,也按照网上说的设置了setlocale(LC_CTYPE, "chs" ),不过没效果。
这是一条镜像帖。来源:北邮人论坛 / cpp / #31075同步于 2009/11/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
求教关于CStdioFile读取Unicode编码文件的问题
Wing
2009/11/10镜像同步6 回复
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
来给个最简单的
//txt.h
#pragma once
#include "stdafx.h"
namespace Texting
{
class Txt
{
public:
Txt(void);
virtual ~Txt(void);
enum Encoding
{
ASCII = 0,
UTF16 = 1,
};
static std::wstring read_file(std::wstring const& file);
static void write_file(std::wstring const& file, Encoding encod, std::wstring const& str);
protected:
enum EncodingFlg
{
FLG_UTF16 = 0xFEFF,
};
};
}
#include "StdAfx.h"
#include "Txt.h"
namespace Texting
{
Txt::Txt(void)
{
}
Txt::~Txt(void)
{
}
std::wstring Texting::Txt::read_file( std::wstring const& file )
{
using namespace std;
wstring str;
wifstream ifs(file.c_str(), ios_base::binary);
wchar_t flg_tmp[2];
ifs.read(flg_tmp, 2);
WORD flg = (flg_tmp[0] & 0xFF) + ((WORD)(flg_tmp[1] & 0xFF)<< 8);
size_t char_width; // 一个字符占几字节
switch (flg)
{
case FLG_UTF16:
ifs.seekg(2);
char_width = 2;
break;
default:
ifs.seekg(0); // 从头读文件
char_width = 1;
break;
}
wchar_t buffer[2] = {0, 0};
while (ifs.read(buffer, char_width))
{
str += (buffer[0] + (buffer[1] << 8));
}
return str;
}
void Texting::Txt::write_file( std::wstring const& file, Encoding encod, std::wstring const& str )
{
using namespace std;
WORD flg;
switch (encod)
{
case ASCII:
{
wofstream of(file.c_str());
of << str;
assert(of.good());
of.close();
return;
}
break;
case UTF16:
flg = FLG_UTF16;
break;
}
ofstream of(file.c_str(), ios_base::binary | ios_base::out);
of.write((char*)&flg, sizeof(flg)); // 写入BOM
assert(of.good());
of.write((char*)str.c_str(), str.length() * 2); // 写入数据
assert(of.good());
of.close();
}
}
........................
【 在 ericyosho 的大作中提到: 】
: 啊,switch switch,我看到了多态的缺失。
: 这两天看重构,脑子定势了……