- ..clause 1..: Text and binary
- ..clause 2..: Opening and shutting of file
- ..clause 3..: Writing in file
- ..clause 4..: Reading from file
As the most basic distinction though there are a variety of kinds of one in the file
There is a difference of text or binary.
All files are binary essentially files.
Though it is a meaning of binary number when the binary is translated literally
File binary file recorded by binary number as its name suggests
In short, it means the file recorded only by the numerical value.
On the other hand, the text file is a file where only the character string was recorded.
In the computer though the character string because it is shown by the numerical value
The text file is a binary essentially file.
However, because the text file is recorded by the character string
It is easy to correct it with the text editor etc.
Though the binary file can be inspected and be corrected in a specific editor
Because all data becomes hardening of the numerical value
Even if contents are seen, the meaning is not understood at all.
However, because the numerical value is written directly, the size is few and high-speed.
In general, when it is necessary to facilitate handling, the text file.
When high speed is needed, the binary file is often used.
It can be a text, a binary, and can there is no change in a basic procedure of the file manipulation.
If it is understood to have explained here, the binary file can be opened and shut.
The following program is an example of opening the file of the name called test.dat for writing.
When this program is executed, the file of the name of test.dat is made.#include <stdio.h> int main(void) { FILE *file; file = fopen("test.dat","wb"); fclose(file); return 0; }
[ It is possible to use it even if it confuses it ].
Actually, the text data can be read and written even if it opens by the binary.
An inconvenient point increases by treating changing line though the opposite is also possible.
Use the fwrite function to write the numerical value directly in the file.
Substitute the written numerical value for the variable, and specify the address of the variable.fwrite(書き込む変数アドレス,1項目のサイズ,項目数,ファイルポインタ);
Though the value is written in the test.dat file when this program is executed#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; }
If the file is opened with a binary editor, it becomes the following.
Because LSIC86 is 16 bit compiler, the numerical value is written by four bytes.
Because other compilers are 32 bits, it is written by eight bytes.
[ test.dat ]Most binary editors must display the numerical value by the hexadecimal number.
LSIC86 64 00
Additionally, 64 00 00 00.
In writing by the fwrite function, the array can be written by one degree.
[ Little endian and big endian ]
In mathematics, when 100 is converted into the hexadecimal number, it becomes 0064.
However, it is 6400 as a result of the inspection with the binary editor.
This is a feature of the Intel compatible CPU, and the expression that is called a little endian.
Delimit the hexadecimal number to two digits and preserve it in this expression in reverse the order.
On the other hand, in Macintosh CPU made by IBM is used.
Here is a method expressed in the order of the hexadecimal number, and it is called the big endian.
[ Do you remember ].
The array identifier is not [wotsuke] in the expression because it becomes an initial address.
When this program is executed, the value is written in test.dat.#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; }
[ test.dat ]
LSIC86 0A 00 64 00 E8 03 10 27
Additionally, 0A 00 00 00 64 00 00 00 E8 03 00 00 10 27 00 00.
Use the fread function to read the numerical value of the file directly.
The usage of the fread function is as follows.
When this is seen, it is understood that the usage is the same as the fwrite function.fread(読み込む変数のポインタ,1項目のサイズ,項目数,ファイルポインタ);
Though the execution result of this program changes depending on the content of the test.dat file#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; }
[ test.dat ]
0A 00 00 00
The array can be read by doing as well as the fwrite function.
10