MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

Reading and writing binary files

Text and Binary
There are many different types of files, but the most basic distinction is that
There is a difference between text and binary.

All files are essentially binary files.
If you translate binary directly, it means "binary number.
A binary file is, as the name implies, a file recorded in binary numbers, a
In short, it means a file that is recorded only numerically.

In contrast, a text file is a file containing only strings.
In a computer, even strings are represented numerically.
A text file is also essentially a binary file.
However, because text files are recorded as strings, they are not
It is easy to modify with a text editor, for example.

Binary files can also be viewed and modified with certain editors, but
Because all data is a mass of numbers, the
Even if you look at the contents, you have no idea what they mean.
However, because it writes numerical values directly, it is small in size and fast.

In general, if you need to facilitate handling, you can use a text file, a
Binary files are often used when high speed is required
File Open/Close
Whether text or binary, the basic procedure for file manipulation remains the same.
Binary files are also opened and closed using the fopen and fclose functions.
The method of specifying the file name and mode is exactly the same.
However, when opening a binary file, a b is added to the end of the mode string.

If you understand what we have described so far, you can open and close binary files.
The following program is an example of opening a file named test.dat for writing.

Source Code
 #include <stdio.h>

int main(void)
{
    FILE *file;
    file = fopen("test.dat", "wb");
    fclose(file);
    return 0;
}

When this program is run, a file named test.dat is created.
In this case, the contents are empty, of course, because we just opened it.

You can use them mixed together.
In fact, you can open it in binary and still read and write text data, and
The reverse is also possible, but with more inconveniences such as the handling of line breaks.

Writing to file
To write numbers directly to a file, use the fwrite function.
The usage of the fwrite function is as follows

fwrite function
 fwrite(address of variable to write, size of one item, number of items, file pointer);

Assign the number to be written to a variable and specify the address of the variable.
The size of an item can be determined using the sizeof operator.
If you only want to write variables, the number of items can be 1 for now.
The following program writes the number 100 to the test.dat file.

Source Code
 #include <stdio.h>

int main(void)
{
    int buf = 100;
    FILE *file;
    file = fopen("test.dat", "wb");
    fwrite(&buf, sizeof(buf), 1, file);
    fclose(file);
    return 0;
}

When this program is run, values are written to the test.dat file, but the
This value cannot be viewed in a typical text editor.
To view binary files, a binary editor is required.
The following is the author's favorite editor.

hgBed http://www.vector.co.jp/soft/win95/util/se081906.html

If you open the file using a binary editor, you will see the following
Since the int type is 32 bits in modern compilers, it is written in 8 bytes.

test.dat" opened in a binary editor
64 00 00 00

The binary editor displays numbers in hexadecimal.
Therefore, the number 100 is displayed as 64.

Little-endian and big-endian
In mathematics, converting 100 to hexadecimal results in 0064.
However, the result viewed in a binary editor is 6400.
This is a feature of Intel-compatible CPUs, a representation called little endian.
In this representation, hexadecimal numbers are separated into two digits and stored in reverse order.

On the other hand, there are CPUs that adopt the method of representing hexadecimal numbers in the same order.
This is called big-endian.

Little-endian may look confusing.
The advantage is that meaningful numbers tend to be at the beginning, which is advantageous for reading data.
Little-Endian is the mainstream today.

Writing with the fwrite function also allows the array to be written once.
To do this, simply specify an array instead of a variable.
The following program writes the value of an array.

Do you remember
The array name is the first address in the expression, so do not put &.


Source Code
 #include <stdio.h>

int main(void)
{
    int buf[] = {10, 100, 1000, 10000};
    FILE *file;
    file = fopen("test.dat", "wb");
    fwrite(buf, sizeof(buf), 1, file);
    fclose(file);
    return 0;
}

When this program is executed, values are written to test.dat.
Opening the file using a binary editor, you will see the following

test.dat" opened in a binary editor
0A 00 00 00 00 64 00 00 00 E8 03 00 00 00 10 27 00 00

Read from file
Use the fread function to read the numeric values directly from the file.
The usage of the fread function is as follows

fread function
 fread(pointer to variable to be read, size of one item, number of items, file pointer);

This shows that the usage is the same as the fwrite function.
The following program reads a numeric value of type int from the test.dat file.

Source Code
 #include <stdio.h>

int main(void)
{
    int buf;
    FILE *file;

    file = fopen("test.dat", "rb");
    fread(&buf, sizeof(buf), 1, file);
    fclose(file);

    printf("%d\n", buf);

    return 0;
}

The results of running this program will vary depending on the contents of the test.dat file, but
If written by the program created in section 3, the following would be true
If the contents of the test.dat file were as follows, the execution result would be as follows

Binary editor "test.dat" like this
0A 00 00 00


Execution Result
10

Arrays can also be read in the same way as with the fwrite function.

Binary and text file usage
The advantages of binary files are faster reading and writing and smaller file size.
Therefore, files that should be handled as quickly as possible, such as images, videos, and sounds, are basically binary files.

In contrast, the advantage of text files is that they are easy for humans to edit.
If you are in trouble, a text file is useful because it is easy to handle.



About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box