※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Reading and writing of binary file   

  1. ..clause 1..: Text and binary
  2. ..clause 2..: Opening and shutting of file
  3. ..clause 3..: Writing in file
  4. ..clause 4..: Reading from file

[1] Text and binary

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 returns to contents.


[2] Opening and shutting of file

It can be a text, a binary, and can there is no change in a basic procedure of the file manipulation.
The binary file opens and shuts by using the fopen function and the fclose function, too.
The method of specifying the file name and the mode is also quite the same.
However, apply b at the end of the mode character string in the place where the binary file is opened.

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.

 
#include <stdio.h> 

int main(void)  
{
	FILE *file;  
	file = fopen("test.dat","wb");  
	fclose(file);  
	return 0;
}
When this program is executed, the file of the name of test.dat is made.
Naturally, contents are empty only it is to open this time.

[ 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.

It returns to contents.


[3] Writing in file

Use the fwrite function to write the numerical value directly in the file.
The usage of the fwrite function is as follows.

fwrite(書き込む変数アドレス,1項目のサイズ,項目数,ファイルポインタ);
Substitute the written numerical value for the variable, and specify the address of the variable.
The size of the item can be requested by using the sizeof operator.
Do not care about the number of items for the time being by one only if you write the variable.
The following program : to the test.dat file Write the numerical value of 100.
 
#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;
}
Though the value is written in the test.dat file when this program is executed
This value cannot be seen with a general text editor.
In seeing the binary file, the binary editor is needed.
The following are editors that the author is patronizing.
hgBed http://www.vector.co.jp/soft/win95/util/se081906.html

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  ]
LSIC86 64 00
Additionally, 64 00 00 00.
Most binary editors must display the numerical value by the hexadecimal number.
Therefore, the numerical value of 100 is displayed as 64.

[   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.
In writing by the fwrite function, the array can be written by one degree.
The way only specifies the array instead of the variable.
The following program is a program that writes the value of the array.

[ Do you   remember   ].
The array identifier is not [wotsuke] in the expression because it becomes an initial address.
 
#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, the value is written in test.dat.
If the file is opened with a binary editor, it becomes the following.
[  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.

It returns to contents.


[4] Reading from file

Use the fread function to read the numerical value of the file directly.
The usage of the fread function is as follows.


fread(読み込む変数のポインタ,1項目のサイズ,項目数,ファイルポインタ);
When this is seen, it is understood that the usage is the same as the fwrite function.
The following program reads the numerical value of the int type from 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;
}
Though the execution result of this program changes depending on the content of the test.dat file
It becomes as follows when written by the program made in clause 3.
The execution result becomes as follows when as follows contents of the test.dat file.
[  test.dat  ]
0A 00 00 00

10
The array can be read by doing as well as the fwrite function.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.