C++ 文本文件和二进制文件的区别
文本文件是以 ASCII字符形式存储数据的文件,通常用于存储字符流。文本文件按行组织,每行以字符('\n') 结尾。源代码文件本身就是文本文件。
二进制文件是将数据存储在文件中的方式与存储在主存储器中进行处理的方式相同。它以二进制格式而不是 ASCII字符存储。它通常用于存储数字信息(int、float、double)。通常二进制文件只能在程序中创建,其内容只能由程序读取。
文本文件与二进制文件
以下是文本文件和二进制文件之间的一些区别。 Text files save the data by converting each digit in data into ASCII format which will take up much of the space as compared to the required one. For example, the number 546378 is an integer that should occupy 4 bytes in the disk but it will occupy 6 bytes, 1 byte for each digit in the number. These save memory because the data of any type will get stored in memory as per its memory size. For example, any integer number irrespective of individual digits in the number will be stored by consuming 4 bytes.S. No. Text file Binary File 1. The text files can easily be transferred from one computer system to another. Binary files cannot easily be transferred from one computer system to another due to variations in the internal variations in the internal representation which varies from computer to computer. 2. It stores data using ASCII format i.e. human-readable graphic characters. It stores data in binary format i.e. with the help of 0 and 1. 3. These files are easily readable and modifiable because the content written in text files is human readable.
Content written in binary files is not human-readable and looks like encrypted content.These files are not easily readable and modifiable because the content written in binary files is not human-readable and it is encrypted content. 4. These files create portability problems. These files are easily portable. 5. 6. Any file is by default text file. The ios:: binary mode has to be used with binary files while opening them. 7. Error in a textual file can be easily recognized and eliminated. Error in a binary file corrupts the file and is not easily detected. 8. In a text file, a new line character is first converted to a carriage return-line feed combination and then written to the disk. Vice versa happens when a line is read from the text file. In binary file, no such conversion from newline to carriage return-line feed combination is done. 9. In a text file, a special character with ASCII code 26 is inserted at the end of the file. This character signals the EOF to the program when encountered. There is no such special character in the binary file to signal EOF.