读书人

wofstream输出unicode文件的有关问题

发布时间: 2012-04-03 12:38:19 作者: rapoo

wofstream输出unicode文件的问题
VS2008

std::wstring str = L"s";
std::wofstream ofile("F:\\test.txt");
ofile<<str;

为什么文件的内容是ss?似乎wofstream并没有输出unicode格式的流,而是把要输出的内容按ANSI输出了2遍。
另外wofstream的构造函数可以接受char的文件名也可以接受wchar_t的文件名,这两个有什么区别?

[解决办法]
这是因为输入输出流的缓冲区是char型的, 因此输出wchar_t就必须进行转换.
在"ofile <<str"; 使用 "单步调式", 你可以看到在写入文件之前, VC对str的内容进行了编码转换

如果想以unicode的格式输出, 在VC2008里可以使用以下方法

C/C++ code
// unicode_basic_filebuf.cpp// compile with: /EHsc// ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_vcstdlib/html/3196ba5c-bf38-41bd-9a95-70323ddfca1a.htm#include <iostream>#include <string>#include <fstream>#include <iomanip>#include <memory.h>#include <string.h>#define IBUFSIZE 16using namespace std;void hexdump(const string& filename);int main(){    wchar_t* wszHello = L"Hello World";    wchar_t wBuffer[128];    basic_filebuf<wchar_t> wOutFile;    // Open a file, wcHello.txt, then write to it, then dump the    // file's contents in hex    wOutFile.open("wcHello.txt",        ios_base::out | ios_base::trunc | ios_base::binary);    if(!wOutFile.is_open())    {        cout << "Error Opening wcHello.txt\n";        return -1;    }    wOutFile.sputn(wszHello, (streamsize)wcslen(wszHello));    wOutFile.close();    cout << "Hex Dump of wcHello.txt - note that output is ANSI chars:\n";    hexdump(string("wcHello.txt"));    // Open a file, wwHello.txt, then set the internal buffer of    // the basic_filebuf object to be of type wchar_t, then write    // to the file and dump the file's contents in hex    wOutFile.open("wwHello.txt",        ios_base::out | ios_base::trunc | ios_base::binary);    if(!wOutFile.is_open())    {        cout << "Error Opening wwHello.txt\n";        return -1;    }    wOutFile.pubsetbuf(wBuffer, (streamsize)128);    wOutFile.sputn(wszHello, (streamsize)wcslen(wszHello));    wOutFile.close();    cout << "\nHex Dump of wwHello.txt - note that output is wchar_t chars:\n";    hexdump(string("wwHello.txt"));    return 0;}// dump contents of filename to stdout in hexvoid hexdump(const string& filename){    fstream ifile(filename.c_str(),        ios_base::in | ios_base::binary);    char *ibuff = new char[IBUFSIZE];    char *obuff = new char[(IBUFSIZE*2)+1];    int i;    if(!ifile.is_open())    {        cout << "Cannot Open " << filename.c_str()             << " for reading\n";        return;    }    if(!ibuff || !obuff)    {        cout << "Cannot Allocate buffers\n";        ifile.close();        return;    }    while(!ifile.eof())    {        memset(obuff,0,(IBUFSIZE*2)+1);        memset(ibuff,0,IBUFSIZE);        ifile.read(ibuff,IBUFSIZE);        // corner case where file is exactly a multiple of        // 16 bytes in length        if(ibuff[0] == 0 && ifile.eof())            break;        for(i = 0; i < IBUFSIZE; i++)        {            if(ibuff[i] >= ' ')                obuff[i] = ibuff[i];            else                obuff[i] = '.';            cout << setfill('0') << setw(2) << hex                 << (int)ibuff[i] << ' ';        }        cout << "  " << obuff << endl;    }    ifile.close();} 

读书人网 >C++

热点推荐