1

So, I have a class

class MySuperClass { public: std::string buffer; }; 

And wants to print buffer to std::cout. Here's some code about filling string from a file:

MySuperClass msc; std::fstream file("./favicon.ico", std::fstream::in | std::fstream::binary); if (file.is_open()) { std::stringstream ss; while (!file.eof()) ss << static_cast<uint8_t>(file.get()); msc.buffer.assign(ss.str()); file.close(); } 

When I output string, all ok. It prints messy, but something. But when I call c_str() on the buffer and try to print, it prints nothing.

2
  • 2
    @phuclv your comment should've been answer. Commented Oct 15, 2020 at 7:24
  • You should not expect to see anything sensible when you do this. Not all byte sequences can be interpreted as text at all, depending on the encoding you choose; and certainly in generally they will not be text that has any meaning. Commented Oct 15, 2020 at 13:38

1 Answer 1

4

An ICO file starts with 2 zero bytes so buffer.c_str() points to an empty string. As a result printing it shows nothing. If you print the byte values you'll immediately see why

for (const auto c: buffer) std::cout << std::setfill('0') << std::setw(2) << std::hex << +c << ' '; 

OTOH std::string can contain embedded nulls, printing it outputs the whole file to the output stream

But why do you print a binary file as text in the first place? It's a very bad idea and may mess up the terminal completely because there may be control characters in the byte stream and you can't ever see normal texts anymore until you reset the terminal. Besides you should read data in big blocks instead which is much faster than operating char-by-char

See

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.