とらりもんHOME  Index  Search  Changes  Login

とらりもん - C intro 6. file input/output Diff

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

In [[C intro 2. for satement]], we have learned to output to a file rather than to a display, using a "redirect" of "the standard input / output" (stdio). In such ways, redirect is convenient in Unix/Linux programming. It is because you can use it by pipe also. You can connect multiple programs by pipes.

However, without using standard input / output, it is often that you want to get and write data with a specific file directly. Especially when trying to read data from multiple files or output to multiple files, it can not be realized with standard input / output.

Here let's learn how to exchange data with a specific file directly without using standard input / output. As an example, consider a program that prints integers from 0 to 9:

    /* number.c */
    /* 2018/07/15 K. Nasahara */
    # include <stdio.h>

    main()
    {int i;
     for (i=0; i<10; i++) printf("%d\n", i);
    }

After compiling and running this program, numbers 0 to 9 appear on the screen. If you redirect this to a file, you can create a text file with numbers from 0 to 9:

$ ./number > result.txt

We modify this program and output directly to a file called out.txt (without using a redirect). The part you should change is "main" as follows:

    main()
    {int i;
     FILE *fp;
     fp=fopen("out.txt", "wt");
     for (i=0; i<10; i++) fprintf(fp, "%d\n", i);
     fclose(fp);
    }

Compile and run it, then you will see that a file named out.txt is created, in which the numbers from 0 to 9 are written.

Let's explain the above modified part. First of all, in order to handle files, we need to prepare a "file pointer". The file pointer is like an alter ego of a file in a program. If you want to write something to a file in the program, declare the file pointer of that file and instruct the program to "write it to this pointer" instead of "write it to this file". Even when you want to read something from a file, you specify the file pointer of that file and command "read from this pointer" rather than "read from this file".

In the above program, the place for preparing the file pointer is:
FILE *fp;
where, "FILE *fp;" declares a variable of "file pointer type". You do not have to ask "what is it!? Why FILE is capital letters? What is the meaning of the asterisk?", etc. Someday in the future, you will understand them if you keep learning C language more.

The next line
fp=fopen("out.txt", "wt");
connets the file pointer "fp" to the real file named "out.txt". After this, the program treats this file by fp. The message "wt" indicates a "write" mode.

The next line
for (i=0; i<10; i++) fprintf(fp, "%d\n", i);
writes data to the file. Instead of printf, we use fprintf to output something to a file. The first parameter of fprintf (which is fphere ) fp here) is the file pointer of the output file. For your interest, instead of scanf, fscanf is used for input from a file.

The next line
fclose(fp);
closes the file and delete the file pointer.

!!標準入出力のファイルポインタ

 実は, 標準入出力も, 一般のファイルと同じように, Cプログラムのなかでファイルポインタで指定することで, あたかもファイルのようにあつかうことができる。標準入力のファイルポインタはstdin, 標準出力のファイルポインタはstdoutである。これらは宣言したりfopen, fcloseなどをする必要はない。

 ためしに, 上のプログラムで, fprintfの中のfpをstdoutにかえてコンパイル・実行してみよ。
!!File pointer of standard input / output (stdio)
There are file pointers of standard input and output, which are "stdin" and "stdout", respectively.


 printfやscanfを使えば, わざわざ標準入出力のファイルポインタなどを使わなくてもいいじゃないかと思うかもしれない。それはそのとおりである。しかし, Cの関数として, バイナリファイルを入出力する, freadやfwriteという関数があり, これらは画像解析や偏微分方程式の解析などで非常によく使われるが, これらの関数は, 必ずファイルポインタを指定しなければならない。そのような関数に対して標準入出力を割り当てる場合に, stdinやstdoutというファイルポインタが欠かせないわけである。Exercise 6-1: Try to change fp to stdout in the above program. What happens?

You may think these file pointers like stdin and stdout are useless because we can just use printf and scanf instead of fprintf and fscanf. Sometimes yes, but sometimes no. Especially when you treat binary data, you need to use functions such as "fread" and "fwrite", which always need file pointers. "stdin" and "stdout" are necessary in such cases even if you use standard input / output only.

[Go back to [[C language introduction]]]