※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | 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 text file   

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

[1] Handling of file

Everything has been displayed on the screen up to now when inputting and outputting.
It is because of the understanding of the result at once if it displays it on the screen and convenience.
However, it disappears as a result of the display on the screen when the program ends.
Moreover, it is not realistic to display a huge amount of data on the screen.

It is usual to preserve data in the file in such a case.
Because the data preserved as a file is preserved on the disk
It doesn't disappear, and the copy and the reorganization collection are easy.

Here, it explains the method of reading and writing the file by C language.

It returns to contents.


[2] Opening and shutting of file

The procedure for operating the file from the program is roughly done in following order.

  - > that opens the file It is read and written   - > to the   file Shut the   file.
In a word, the opening and shutting of the file is indispensable to the file manipulation.
Then, the function that opens and shuts the file is prepared in C language.
The opening function The fopen function and the shutting function : It is fclose function.
The include of stdio.h is needed to use this function.
The usage of each function becomes as follows.

FILE型のポインタ変数 = fopen(ファイル名,モード);
fclose(FILE型のポインタ変数);
The file name is a file name as shown in the name.
It is possible to specify in the full path, and to specify it only by the name.

The mode is a character string that shows the purpose to open the file.
Specify one [doreka] in the following six kinds of character strings for the mode.
Mode character string Purpose
r Reading. It fails in there is no file.
r+ Reading and writing. It fails in there is no file.
w Writing. Make an empty file even if there is a file.
w+ Reading and writing. Make an empty file even if there is a file.
a Additional writing. Make it when there is no file.
a+ Reading and writing. Make it when there is no file.
The FILE type is a type that is not accustomed to be heard or the true colors is a structure.
When the fopen function is executed, the pointer to the FILE type with the file information is returned.
Only because use this pointer as identifier of open file thereafter
Never do a peculiar operation to the pointer, and use the element of the structure.
The pointer variable to the FILE type will be called a file pointer in the future.

If it is understood to have explained here, the file can be opened and shut.
The following program is an example of opening the file of the name called test.txt for writing.

 
#include <stdio.h> 

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

[ Role of   fclose   ]
Seemingly, there is neatly a role though it is a meaningless fopen function.

In the environment such as Windows, Unix(Linux), and Mac where two or more software moves at the same time,
When you rewrite the same file at the same time with two set of software
Because it doesn't understand which to be reflected
To the file that opens to write it by the fopen function
It locks it so that it is not rewritten with other software.
The fclose function should be able to be used from other software by removing the key.

Additionally, preserve it in the memory when you open the file.
In the first time storage on the disk when the fclose function is executed,
It is likely to speed it up.

It returns to contents.


[3] Writing in file

As for the function where the text is written in the file, a lot of kinds are prepared.
There is fprintf function that looks like a familiar printf function well in that.
The usage of the fprintf function is as follows.


fprintf(ファイルポインタ,書き込み文字列,変数・・・);
Though the usage is quite the same as the printf function excluding specifying the file pointer
The specified character string is written in not the screen but the file.
The following program : to the test.txt file Write the character string of Hello and world.
 
#include <stdio.h> 

int main(void)  
{
	FILE *file;  
	file = fopen("test.txt","w");  
	fprintf(file,"Hello,world");  
	fclose(file);  
	return 0;
}
If this program is executed, contents of the test.txt file are as follows.
The test.txt file is made in the same folder as the execution file.
[  test.txt  ]
Hello,world
The value of the variable can be written just like the printf function.
The following program writes the value of variable i in the test.txt file.
 
#include <stdio.h> 

int main(void)  
{
	int i = 100;  
	FILE *file;  
	file = fopen("test.txt","w");  
	fprintf(file,"%d",i);  
	fclose(file);  
	return 0;
}
If this program is executed, contents of the test.txt file are as follows.

100

[   Reading and addition   ]
Anything doesn't occur even if the function for writing is used when opening in the reading mode.
Data is added at the end of the original file when opening by the addition mode.

It returns to contents.


[4] Reading from file

Though there are a lot of kinds of functions that read the text of the file, too
There is fscanf function that looks like a familiar scanf function.
The usage is the same as the scanf function excluding specifying the file pointer at the head.

Though it becomes an input waiting when executing it to read the scanf function from the key boat
In the fscanf function, read the text of the file from the head.
The following program displays the first figure by the test.txt file reading.

 
#include <stdio.h> 

int main(void)  
{
	int i;  
	FILE *file;  
	file = fopen("test.txt","r");  
	fscanf(file,"%d",&i);  
	fclose(file);  
	printf("%d\n",i);  
	return 0;
}
The execution result of this program is different according to contents of the test.txt file.
The execution result becomes as follows when as follows contents of the test.txt file.
[  test.txt  ]
100

100
Because the texts other than the figure are disregarded when %d finger fixed child is used like the example above
The execution result becomes as follows when as follows contents of the test.txt file.
[  test.txt  ]
test100

100
If %s finger fixed child who uses it to input the character string is used, the entire character string can be read.
However, when space etc. are included, it is read there.
[  test.txt  ]
test100

test100
Moreover, if two or more numerical values are delimited by comma (,) and arranged
It is also possible to read two or more variables.
 
#include <stdio.h> 

int main(void)  
{
	int i,j;  
	FILE *file;  
	file = fopen("test.txt","r");  
	fscanf(file,"%d,%d",&i,&j);  
	fclose(file);  
	printf("i = %d : j = %d\n",i,j);  
	return 0;
}
[  test.txt  ]
23,56

i = 23 : j = 56
The file that delimits in this manner by comma (,) and arranges the numerical value and the character string is called Comma Separated Value.
It is known as a general file format that can be treated by the spreadsheet of Excel etc.

It returns to contents.


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