Concept
When should data be stored as binary or text files?
ComputerScienceOne / Buffered and Unbuffered
"9.1.4. Binary vs Text Files As previously mentioned, files can be stored as pure binary data or as plaintext (ASCII). Depending on our application and the nature of the data being written to files, the choice of which to use may be clear. If we want the data in our files to be human-readable, then we need to store them as plaintext. However, in general, we should prefer storing data in a binary format. The reason for this is that binary generally requires less space and is more efficient to process. Consider as an example, storing a collection of integers in a file. Each integer requires 4 bytes when represented in binary. However, when represented as a string, it requires as many bytes as there are digits in the number. For example, the maximum representable value of such an integer would be 2 31 − 1 = 2 , 147 , 483 , 647, requiring 10 ASCII characters and thus 10 bytes to represent, two and a half times as much memory as with the binary representation. Further, if a lot of numbers are stored, each number (as a string) would need to be delimited by yet another character. With a binary representation, no delimiter would be necessary as we would know that each 4 byte block represents a single number. This disparity can be even more stark with other types such as floating-point numbers."
Related Ideas
- When should data be stored in binary or text file format?ComputerScienceOne · Binary vs Text Files
- How can file input/output programs process text, binary data, and word searches?ComputerScienceOne · Exercises
- How do fread and fwrite read and write binary data?ComputerScienceOne · Binary Files
- How do fprintf() and fscanf() read from and write to plaintext files?ComputerScienceOne · Plaintext Files
- How do you write and read binary data in C?ComputerScienceOne · Closing Files
- How can you write binary data to an output file?ComputerScienceOne · File Output
- How can you read a text file one line at a time?ComputerScienceOne · Binary Files
- When should you use specialized classes for binary file input?ComputerScienceOne · File Input