What is the difference between text and binary modes?

Streams can be classified into two types: text streams and binary streams. Text streams are interpreted, with a maximum length of 255 characters. With text streams, carriage return/line feed combinations are translated to the newline n character and vice versa. Binary streams are uninterpreted and are treated one byte at a time with no translation of characters. Typically, a text stream would be used for reading and writing standard text files, printing output to the screen or printer, or receiving input from the keyboard.  A binary text stream would typically be used for reading and writing binary files such as graphics or word processing documents, reading mouse input, or reading and writing to the modem.  

Showing Answers 1 - 1 of 1 Answers

kbjarnason

  • Jul 2nd, 2010
 

The primary differentiation between text and binary modes as far as C is concerned has to do with line endings.? Take a look at a printf statement; note that many of them end in a n.? n is not, as one might think, a specific character; it is an escape sequence, representing "end of line".

This is an important concept, particularly as pertains to files.? Here's why.? Some OSen (Windows, DOS) use a two-character sequence, ascii 10 and ascii 13, to indicate end of line.? Some OSen use a single character - but which one varies.? Some may even, presumably, use ascii 10 and 13, but in opposite order, or use entirely different and weirder things.

Here's the kicker: the standard C library formatted I/O functions (printf, scanf, putchar, getchar, etc) have much magic in them, which lets them sort out just what the heck is really going on.? You write a n to a file, they invoke their magic and convert it to whatever makes sense for that particular implementation/OS combination.? You read a file, they again invoke their magic to undo whatever chaos is involved in end of line handling and turn it all into a nice, neat litle n escape for you to deal with.

Here's the gotcha... what if I'm trying to, oh, copy a file?

Yeah, sure, I read it in, write it out, all is good... except...? if the file is not in fact a text file, the one thing I do *not* want to happen is for any sort of weird translation to be going on; I want an exact, byte-for-byte copy, not have my program files magically transformed into unusable junk, thank you very much.

By using binary mode, I effectively turn off all the translation.? When I read a character, what I get is whatever was in the file.? When I write a character, that's what goes in the file.? No mucking about, no translation, all's good.

There are a couple other gotchas to be concerned about, too.? For example, in DOS (and possibly Windows), if you open a file in text mode and it contains a particular character value (ascii 26, I think it is), the library treats it as end of *file*.? Never mind there's 17 more megabytes of data to be read, you opened it in text mode, an end-of-file character was found, that's that.

Here's another gotcha: seeking.? Suppose you want to seek to position, oh, 593 in a file - in text mode.? What, precisely, does that 593 mean?? Yes, fine, it means you want? to go 593 bytes in from the start of the file... but is that 593 bytes before or after translating end of line characters and checking for end of file?? If you factor in the end of line changes alone, that 593 could easily by 100 or more bytes off from where you expected to be.

By contrast, if you're trying to read something in such that it's useful for a human, you almost certainly don't want binary mode, as this will happily read in (and presumably later display) all those screwball codes which _should_ have been sorted out to be end of line sequences and converted to a more useful n.

So there you have it.? The key difference between text and binary mode is, in binary mode the data is not translated or modified or decoded, which is great for copying files and the like, while in text mode the data _is_ translated and modified and decoded and whatnot, which is less good for copying, but a whole lot nicer for handling readable data.

Oh, and as an aside... if you're going to use fread/fwrite or an equivalent, you almost certainly want binary mode.? Consider what would happen if you used fwrite to write out, oh, an int variable, but one of the bytes was interpreted as an end-of-line value or something similar; what actually gets into the file, in text mode, may very well be not what you wanted in it.? Same deal for fread... you don't want your binary data translated, use binary mode.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions