Why use unsigned chars for writing to binary files? And why shouldn't stream operators be used to write to binary files?

佐手、 提交于 2019-12-13 03:35:44

问题


My first question is, why is it customary to use unsigned chars for writing to files in binary mode? In all of the examples I have seen, any other numerical value is casted to unsigned char before writing to the binary file.

My second question is, what's so bad about using stream operators to write to binary files? I've heard that read() and write() operators are best used for writing to binary files, but I don't really understand why that's the case. Using stream operators to write to binary files works fine for me IF I first cast the value to unsigned char.

float num = 500.5;
ostream file("file.txt", ios::binary);

file << num  // results in gibberish when I try to read the file later
file << (unsigned char)num  // no problems reading the file with stream operators

Thanks in advance.


回答1:


chars are the smallest type in C/C++ (by definition, sizeof( char ) == 1). Its the usual way to see objects as a sequence of bytes. unsigned is used to avoid signed arithmethic to get in the way, and because it best represents binary contents (a value between 0 and 255).

To operate on binary files, streams provide the read and write functions. The insertion and extraction functionality is formatted. It's working for you just by chance, for instance if you output an integer with << then it will actually output the textual representation of the integer value and not its binary representation. In your provided example, you cast a float to an unsigned char before outputing, actually casting the real value to a small integer. What do you get when you try to read the float back from the file?




回答2:


Because all the overloads of operator<< are called formatted functions. They format the data before writing to the output file. In other words, they cannot be used if you want to write binary data to file. Binary data can be written to file with unformatted functions - those which don't format the data.

std::ostream provides one unformatted output function called write(), with the following signature:

ostream& write ( const char* s , streamsize n );

which also answers other question that:

why is it customary to use unsigned chars for writing to files in binary mode?

No. It is wrong. The function write() accepts const char*, not const unsigned char *.

--

The online doc says about operator<<:

This operator (<<) applied to an output stream is known as insertion operator. It performs an output operation on a stream generally involving some sort of formatting of the data (like for example writing a numerical value as a sequence of characters).

and it says about write():

This is an unformatted output function and what is written is not necessarily a c-string, therefore any null-character found in the array s is copied to the destination and does not end the writing process.




回答3:


The reason to use unsigned char is that it is guaranteed to be unsigned, which is very much desirable when it comes to bitwise operations -- which can come in handy when manipulating binary data. You have to keep in mind that char (also known as plain char) is a separate type from unsigned char and it is not specified whether this is a signed or unsigned type.

Finally, the formatted functions of streams are designed to output/parse a textual, human-readable representation of data, where for instance 123456789 could1 be represented as the nine characters "123456789", which can fit in nine bytes. For comparison, a possible binary representation as 0x75BCD15 can fit in four bytes, which is more than twice as compact.

It is not entirely unexpected that what you're doing succeeds, since whether something is a binary file or not is simply determined by what you're doing with it. If you're writing text to the file, it is normal to retrieve that text back later on.

1: depending on e.g. locales, which is another feature specific to formatted functions.



来源:https://stackoverflow.com/questions/7460206/why-use-unsigned-chars-for-writing-to-binary-files-and-why-shouldnt-stream-ope

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!