尝试使用ofstream写一些基本的文本数据

问题描述 投票:0回答:3

我只是想将一些文本数据写到制表符分隔的文件中。这是代码。 Name() 和 File() 返回 CStringW,而 ID() 返回 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();

 }

但是文件并没有得到我期望的输出,而是像这样

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

什么给?

c++ ofstream
3个回答
3
投票

使用

wofstream
,它具有适合
const wchar_t *
的运算符。

wchar_t *
ostream
将使用
operator<<(ostream &, void *)
因为没有
wchar_t *

的运算符

编辑:在 CStringW 上使用 .GetString()。


1
投票

我通常看到的这种处理方式是将 CStringW 转换为 LPCWSTR,如下所示:

myfile << (LPCWSTR)i->Name() << L"\t" << i-ID() << L"\t" << (LPCWSTR)i->File() << endl;

0
投票

你看到了

CString
的原始指针地址,因为那里被视为
void*
.

记录在这里

要将

CString
std::wcout
一起使用,您必须将对象显式转换为
const wchar_t*
,如下例所示:

CString cs("meow");
wcout << (const wchar_t*) cs << endl;
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.