c++ofstream

Trying to use ofstream to write some basic text data


I'm just trying to write some text data out to a tab-delimited file. Here's the code. Name() and File() return CStringW, while ID() returns int,

ofstream myfile(file);

if (myfile.is_open())
{
    v::iterator i = begin();

    while(i != end())
    {
        myfile << i->Name() << L"\t" << i-ID() << L"\t" << i->File() << endl;
       i++;
    }

    myfile.close();

 }

But instead of getting the output I expect, the file looks like this

001554B00043F66840043F668001554F8
001555400043F6685440043F66800155588
001555F00043F6686000043F66800155638
001556B00043F6686240043F668001556F8
001557700043F6686680043F668001557B8
001558300043F6686800043F66800155878
001558E00043F6688560043F66800155928
001559C00043F6688720043F66800155A08
00155A700043F6689480043F66800155AB8
00155B200043F66810440043F66800155B68
00155BD00043F66811320043F66800155C18
00155C800043F66812840043F66800155CC8
00155D300043F66814040043F66800155D78
00155DE00043F66815360043F66800155E28
00155E900043F66815840043F66800155ED8
00155F400043F66816880043F66800155F88
001560180043F66817040043F66800156050
001560C80043F66817360043F66800156110

What gives?


Solution

  • Use a wofstream, which has the appropriate operators for const wchar_t *.

    wchar_t * to ostream will use operator<<(ostream &, void *) as there's no operator for wchar_t *

    EDIT: Use .GetString() on the CStringW's.