- ..clause 1..: Handling of file
- ..clause 2..: Opening and shutting of file
- ..clause 3..: Writing in file
- ..clause 4..: Reading from 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.
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.
The file name is a file name as shown in the name.FILE型のポインタ変数 = fopen(ファイル名,モード); fclose(FILE型のポインタ変数);
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. |
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.
When this program is executed, the file of the name of test.txt is made.#include <stdio.h> int main(void) { FILE *file; file = fopen("test.txt","w"); fclose(file); return 0; }
[ 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.
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.
Though the usage is quite the same as the printf function excluding specifying the file pointerfprintf(ファイルポインタ,書き込み文字列,変数・・・);
If this program is executed, contents of the test.txt file are as follows.#include <stdio.h> int main(void) { FILE *file; file = fopen("test.txt","w"); fprintf(file,"Hello,world"); fclose(file); return 0; }
[ test.txt ]The value of the variable can be written just like the printf function.
Hello,world
If this program is executed, contents of the test.txt file are as follows.#include <stdio.h> int main(void) { int i = 100; FILE *file; file = fopen("test.txt","w"); fprintf(file,"%d",i); fclose(file); return 0; }
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.
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.
The execution result of this program is different according to contents of the test.txt file.#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; }
[ test.txt ]
100
Because the texts other than the figure are disregarded when %d finger fixed child is used like the example above
100
[ test.txt ]
test100
If %s finger fixed child who uses it to input the character string is used, the entire character string can be read.
100
[ test.txt ]
test100
Moreover, if two or more numerical values are delimited by comma (,) and arranged
test100
#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
The file that delimits in this manner by comma (,) and arranges the numerical value and the character string is called Comma Separated Value.
i = 23 : j = 56