In C language, there are many functions related to file read/write, but there are two types of read/write data: Binary and text. The text read and write functions are not mentioned much. As long as formatted input and output fscanf () and fprintf () are used, the problem can be basically solved. Here we mainly talk about the binary file read/write functions fread () and fwrite ().
The function prototypes are:
Size_t fwrite (const void * buffer, size_t size, size_t count, FILE * stream );
Size_t fread (void * buffer, size_t size, size_t count, FILE * stream );
Where
Buffer is the pointer to store data.
Size is the size of a single element (in bytes)
Count is the number of elements.
Stream is a file pointer.
The return value of a function is the number of elements actually read or written.
Note that when you open a file for Binary reading and writing, you need to add "B" next to the read and Writing Method to indicate binary reading and writing. For example, the file opened for Binary writing can be fp = fopen ("out.txt", "wb ");
Files stored in binary files can be kept confidential to a certain extent. If someone else opens the binary code stored in the text editor, ta may see some garbled characters. The reason for this is probably that if we store the original text (char type), others can still see the content in it. This is because char is stored in ASCII format, which can be recognized by the text editor. But other types won't work.
Let's take an example:
For example, int a = 64 (assuming that int occupies two bytes), the 64 binary is 00000000 01000000. If it is opened in text, the editor will try to display a as two characters, A string of 0 ASCII characters and 64 ASCII characters. The ASCII value corresponding to 0 is null and is not displayed; the ASCII value corresponding to 64 is the character @, which we can see.
If we choose to store a in text, the system will not regard a as a number, but as a sequence composed of two characters: '6' and '4 '. The ASCII value of '6' is 54, the binary value is 00110110, the ASCII value of '4' is 52, and the binary value is 00110100. Therefore, the binary value of a's text storage format is 00110110 00110100 (you must understand that all data is actually stored in binary format in computers ).
Of course, the fundamental purpose of binary storage files is to read and write data more quickly, because computers "like" binary. To encrypt data, you must have an encryption algorithm.
From hoolee