mainajaved
mainajaved

Reputation: 8163

Difference between binary and text file in terms of storage?

I want to ask few questions about bits and bytes as I am very confused.

  1. For example, suppose I have a short int x = 345;. I know that short take 16 bits, so when I write the short in a file it is written as char '3' , '4' ,'5' each containing 1 bytes so total of 24 bits. Is there a way to write the number (short integer) in file as short integer taking 16 bits?

  2. Also am I right in my concept? What will be the difference in writing to my file if the file is binary or text in terms of bytes?

Upvotes: 1

Views: 1876

Answers (2)

cnicutar
cnicutar

Reputation: 182609

Yes, there is a way.

uint16_t sh = 345;
fwrite(&sh, sizeof(sh), 1, fp);

In the case you mentioned 345 is written as text (for example ASCII if that's what you use). In the example I posted, the binary representation of sh is written in the file and it will take only 2 bytes.

What will be the difference in writing to my file if the file is binary or text in terms of bytes?

  1. Text write (fprintf)

    0000000: 00110011 00110100 00110101
                3        4        5
    
  2. Binary write (fwrite)

    0000000: 01011001 00000001
    #Little endian. Read as: 00000001 01011001 = 345
    

If interoperabillity is an issue (i.e. you want to send the file to another machine) the text format is a superior choice as it's portable.

Upvotes: 4

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

If you write the value as a string, it will occupy at least three bytes for the three digits; there would often be a newline or space as well to mark the end of the value.

Yes, you can write the value as 2 bytes. One way would be:

fwrite(&x, sizeof(x), 1, fp);

The difference between binary and text is that you can transport the text between different types of machine with almost complete impunity and all the machines will interpret the data the same way. The binary file can only be interpreted on machines that have the same endian-ness (big-endian or non-Intel vs little-endian or Intel). On the other class of machines, you have to swap the order of the bytes to get the binary data interpreted correctly.

Upvotes: 1

Related Questions