As mentioned earlier files are required to store information permanently for future use. The files can be stored in two ways
1. Text Files : Text Files store information in ASCII codes. In text files, each line of the text ends with a special character known as the "End of Line" character (EOL).
2. Binary Files : A binary file is just a file that contains information in the same format in which the information is held in memory. In binary file, there is no delimiter for a line. Also no translations occur in binary files. As a result, binary files are faster and easier for program to read and write than text files. If the file is not needed to be read by a person directly, binary file are the best way to store program information. A binary file is a file of any length that holds bytes with values in the range 0 to 0xff. (0 to 255). These bytes have no other meaning unlike in a text file where a value of 13 means carriage return, 10 means line feed, 26 means end of file and software reading text files has to deal with these. In modern terms we call binary files a stream of bytes and more modern languages tend to work with streams rather than files.How would you know that a file is a text file or a binary file? It's simple. Just try to open the file in any editor like "Notepad" in Windows. If the file opens successfully and you can read the data easily, it's a text file otherwise a binary file.
Difference between a text and a binary file
1. A text file can be easily opened in an editor, whereas a binary file can not be.In C, when you create a binary file you can't read it in the normal way. This means that if a file was created using binary writing function you won't be able to open it using DOS Shell or the File -> open methods (given in the previous posts). Then how would you read such a file? The only method to read a binary file is by creating a program for "Binary Reading".
2. In text files, a special character whose ASCII value is 26 is automatically added to the end of the file. This mark is called the end of file (EOF) marker. When we create a program to read a text based file, it is this symbol which is searched. As soon as this symbol is reached "end of file" is assumed. Opposite to this in binary files, the size of the file is determined from the file's entry in the OS's directory and file table. If a file has been written in text mode it should be read in text mode only. Similarly if a file was created in binary mode, the same should be used for reading.
3. In text mode, a newline character ("\n") is converted into the carriage return-linefeed combination before writing it to the disk. Similarly, the carriage return-linefeed combination on the disk is converted back into a newline when the file is read in text mode. But, if a file is opened in binary mode these conversions will not take place.
4. In text mode, to write numbers in a file, the function available is "fprintf()". The "fprintf()" function stores numbers as string rather that integers. Therefore if you write the values 19567which would have taken 2 bytes as integer will take 5 bytes if written through "fprintf()". This means more memory will be required to even more large numbers. Contrary to this binary functions store data in binary mode which consumes the same amount of disk space as it does in memory.
2. In text files, a special character whose ASCII value is 26 is automatically added to the end of the file. This mark is called the end of file (EOF) marker. When we create a program to read a text based file, it is this symbol which is searched. As soon as this symbol is reached "end of file" is assumed. Opposite to this in binary files, the size of the file is determined from the file's entry in the OS's directory and file table. If a file has been written in text mode it should be read in text mode only. Similarly if a file was created in binary mode, the same should be used for reading.
3. In text mode, a newline character ("\n") is converted into the carriage return-linefeed combination before writing it to the disk. Similarly, the carriage return-linefeed combination on the disk is converted back into a newline when the file is read in text mode. But, if a file is opened in binary mode these conversions will not take place.
4. In text mode, to write numbers in a file, the function available is "fprintf()". The "fprintf()" function stores numbers as string rather that integers. Therefore if you write the values 19567which would have taken 2 bytes as integer will take 5 bytes if written through "fprintf()". This means more memory will be required to even more large numbers. Contrary to this binary functions store data in binary mode which consumes the same amount of disk space as it does in memory.
0 comments:
Post a Comment