Difference between text file and binary file
There are two kinds of storing data in file text format and binary format .
In text file data are stored as line of characters with each line terminated by a new line character (‘\n’). In binary format data is stored on the disk in the same way as it is represented in the computer memory . Unix system does not make any distinction between text file and binary file.
Text file are in human readable form and they can be created and read using any text editor , while binary file are not in human readable form and they can be created and read only by specific program written for them .
The binary data stored in file can not be read using text editor. The hexadecimal value of 1679 is 0x068f , so in binary format it is represented by two bytes 0x06 and 0x8f . In a text file 1679 is represented by the bytes 0x31 , 0x36 , 0x37 ,0x39 ( ASCII values) . NOTE ASCII value for 1 is 31 and for 9 is 39.
Both text file and binary file keep record of the length of the file , and identify the end of file when this is reached . In text files , there is one more way to detect the end of file. The character with ASCII value 26 is considered to be end of file character in text files . All input function stop reading character when this character is encountered and return end of file signal to the program . C does not insert this character in file , it can be entered through the keyboard by Ctrl + Z (Ctrl + D on some systems) . In binary files no such character represent end of file.
In text files newline is stored as a combination of carriage return ‘\r’ (ASCII 13) and linefeed ‘\n’(ASCII 10) while in binary files newline is stored only as ‘\n’(ASCII 10).
In binary format , the data is stored in the same way as it is represented in memory so no conversion s have to take place while transferring of data between memory and file . In text format some conversions have to take place while transferring of data between memory and file. For example while writing to a text file newline (‘\n’) has to be converted to combination of(‘\r’) and (‘\n’) and while reading from text file this combination (‘\r’) and (‘\n’) is to be converted back (‘\n’).
The input and output operations in the binary files takes less time as compared to the text files because no conversion has to be take place . However the data written using binary file system is not very portable because size of data type and byte order may be different on different machines . In text format , these problems do not raise so it is considered more portable