Reading binary files in the C language

Source: Internet
Author: User
Tags fread

Fwrite () is different from fprintf ().

Fwrite saves the written data as the disk contents of the file. fprintf saves the ASCII code that corresponds to each character of the data being written as the disk contents of the file. fprintf did a conversion job.

When the file is opened, Notepad automatically converts the file's disk contents to the corresponding characters as ASCII code, and then displays the text content instead of the disk contents.

For example, when you write "65" to a file with Fwrite, the disk contents of the file are 65 saved (in binary notation on disk). When you open a file with Notepad, Notepad reads 65 and considers 65 as an ASCII code, and then displays the corresponding character "a". So the text you see on the screen is "A".

When using fprintf to write "65" to a file, the disk contents of the file hold the ASCII code corresponding to the characters "6" and "5", respectively 54 and 53. Therefore, the disk contents of the file are 54 and 53. When you open a file with Notepad, Notepad reads 54, and the corresponding "6" is displayed. After reading to 53, the corresponding "5" is displayed.
code example.
#include <stdio.h> #include <stdlib.h>void test () {    FILE *foutput = fopen ("Output", "w");    int A = n;    Char buffer[10000];    int i = 0;    int l;/* Write data to File *    /fwrite (&a, sizeof (A), 1, foutput)    ; fprintf (Foutput, "%d", A);    Fclose (foutput);/* Read data from File *    /foutput = fopen ("Output", "R");    while (fread (buffer + i, sizeof (char), 1, foutput)) {        ++i;    }    L = i;/* Print data on screen *    /for (i = 0; i < L; ++i) {        printf ("buffer[%d] =%d\n", I, (int) buffer[i]); 
   }    printf ("L =%d\n", l);    Fclose (foutput);} int main () {    test ();    return 0;}

Output Result:

Buffer[0] = 65buffer[1] = 0buffer[2] = 0buffer[3] = 0buffer[4] = 54buffer[5] = 53l = 6

Open the display result of the output file:

the definition of fwrite () reference: http://www.cplusplus.com/reference/cstdio/fwrite/

Similarly, fread () and fscanf () also have similar differences.

Fread reads the contents of the file directly into the disk. FSCANF converts the file's disk contents into corresponding characters as ASCII code, and then reads the text content instead of the disk contents.

As you can imagine, if you read the file in the example above with fscanf, read one byte at a time. Then the first 6 characters in buffer will be saved as "a[null][null][null]65".

In addition, it is not useful to feel the open mode "B" of fopen () in the C language. For example, writing "W" or "WB" has no effect on fwritet and fprintf. Previously did not understand what the binary file is going to be, originally fprintf and FSCANF did a conversion.

Reading binary files in the C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.