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

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).

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 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?

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). I don't think that matters. – Nawaz Sep 18 at 8:11 @Nawaz: What?

Avoiding signed arithmetic or getting a value between 0 and 255? Because the later certainly matters to me, and the former prevents bugs like sign-extended conversion to bigger integer types. – K-ballo Sep 18 at 8:15 Are you talking about when using write()?

If so, then it doesn't matter. In fact, using unsigned would give compilation error. – Nawaz Sep 18 at 8:17 @Nawaz: No, we are talking about whats customary.

Using write requires a buffer of the stream's element type, which is char for std::ostream. When I write binary files I use an output stream with an unsigned char for element instead. – K-ballo Sep 18 at 8:24 @K-ballo: What really clicked for me was your mentioning that sizeof(char) == 1.

Of course I knew that, but I never made the connection of how convenient that would be in representing bytes. Thank you. – Terribad Sep 187 at 12:35.

Because all the overloads of operatorBinary 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 operatorIt 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.

Your first paragraph is very much tautological and circular. You mention that a formatted function is not fit to write binary data since they format the data, and that unformatted functions are fit since they do not format. All of which is self-evident.

– Luc Danton Sep 18 at 9:18 @Luc: I think you're saying it because of this >> - those which don't format the data. It merely clarifies what unformatted function means is. One might think that printf is a formatted function because it accepts a formatted string of type const char* as first argument.

This is not what formatted means here. – Nawaz Sep 18 at 9:23 @Nawaz: Thank you for the explanation. Wish there was a "thank you" button.

– Terribad Oct 9 at 12:36 @Terribad: Yes, there is a thank you button. Here on stackoverflow, its name is different; its called Upvote Arrow. – Nawaz Oct 9 at 13:18.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions