欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611
所谓的unicode文件,无非就是在文件头部插入了 0xFFFE的标志。。。读写的时候对应的读写 就可以了。
namespace fileStream
{
bool readFile_Unicode( const string &file ,wstring &destWstring)
{
destWstring.clear();
//setlocale(LC_ALL,"Chinese-simplified");//设置中文环境
locale &loc=locale::global(locale(locale(),"",LC_CTYPE));
std::ifstream filestream (file.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
filestream.seekg (0, std::ios::end);
size_t size = (size_t)filestream.tellg();
filestream.seekg(0,ios::beg);
char* buffer = new char[size + 1];
memset(buffer,0,sizeof(char)*(size + 1));
filestream.read (buffer, size);
destWstring = (wchar_t*)buffer;
destWstring.erase(size/2);//删除末尾可能会出现的乱码 /2 是为了unicode 之后 长度只有一半
filestream.close();
delete[] buffer;
//setlocale(LC_ALL,"C");//还原
locale::global(loc);
return !destWstring.empty();
}
bool writeFile_Unicode( const string &file ,const wstring &writeWstring )
{
//setlocale(LC_ALL,"Chinese-simplified");//设置中文环境
locale &loc=locale::global(locale(locale(),"",LC_CTYPE));
std::ofstream filestream(file.c_str(), std::ios::out | std::ios::binary | std::ios::ate);
filestream.clear();
static const BYTE unicodeHead[]={0xFF,0xFE}; //unicode文件头文件
filestream.write((char *)unicodeHead,2);
filestream.seekp(std::ios::end);
filestream.write((char *)writeWstring.c_str(),writeWstring.length() * 2);
filestream.close();
//setlocale(LC_ALL,"C");//还原
locale::global(loc);
return true;
}
}
欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611