Why does output of a string to unnamed std::ofstream give me a hex number instead?

Viewed 334

I was trying to add some debug output into a C++03 project and got some strange result. Here's the simplified test code:

#include <fstream>

int main()
{
    {
        std::ofstream file("/tmp/test.txt");
        file << "hello" << " ... OK, this works\n";
    }
    std::ofstream("/tmp/test.txt",std::ios_base::app) << "hello"
                                                      << " ... no, I mean hello!\n";
}

For some reason, here's what I get after compilation:

$ g++ test.cpp -o test && ./test && cat /tmp/test.txt
hello ... OK, this works
0x80487fe ... no, I mean hello!

Why do I get a hex number in the case of outputting a string to unnamed std::ofstream object? And why does the subsequent output of a second string work?

1 Answers
Related