C の fprintf() 関数

書式付きファイルの入力と出力 #

ここまでで、ファイルとの間で文字と文字列を読み書きする方法を見てきました。現実の世界では、データはさまざまな種類で構成されています。この章では、さまざまなタイプのデータをフォーマットされた方法で入力および出力する方法を学びます。特定の形式でデータを読み書きする場合は、フォーマットされた入力と出力を使用します。

fprintf() 関数 #

構文: int fprintf(FILE *fp, const char *format [, argument, ...] );

fprintf() 関数は printf() と同じです ただし、コンソールにデータを書き込む代わりに、フォーマットされたデータをファイルに書き込みます。 fprintf() のほぼすべての引数 関数は printf() と同じです ただし、書式設定された出力が書き込まれるファイルへのファイル ポインターである追加の引数があります。成功すると、ファイルに書き込まれた文字の総数を返します。エラーの場合、EOF を返します .

次のプログラムは、fprintf() の使用方法を示しています。 関数。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE *fp;
    char name[50];
    int roll_no, chars, i, n;
    float marks;

    fp = fopen("records.txt", "w");

    if(fp == NULL)
    {
        printf("Error opening file\n");
        exit(1);
    }

    printf("Testing fprintf() function: \n\n");

    printf("Enter the number of records you want to enter: ");
    scanf("%d", &n);

    for(i = 0; i < n; i++)
    {
        fflush(stdin);
        printf("\nEnter the details of student %d \n\n", i +1);

        printf("Enter name of the student: ");
        gets(name);

        printf("Enter roll no: ");
        scanf("%d", &roll_no);

        printf("Enter marks: ");
        scanf("%f", &marks);

        chars = fprintf(fp, "Name: %s\t\tRoll no: %d\t\tMarks: %.2f\n",
            name, roll_no, marks);
       printf("\n%d characters successfully written to the file\n\n", chars);
    }

    fclose(fp);
    return 0;
}

期待される出力:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Testing fprintf() function:

Enter the number of records you want to enter: 5

Enter the details of student 1

Enter name of the student: Tina
Enter roll no: 1
Enter marks: 45

37 characters successfully written to the file

Enter the details of student 2

Enter name of the student: Nina
Enter roll no: 5
Enter marks: 89

37 characters successfully written to the file

Enter the details of student 3

Enter name of the student: Tim
Enter roll no: 2
Enter marks: 49

36 characters successfully written to the file

Enter the details of student 4

Enter name of the student: Jim
Enter roll no: 8
Enter marks: 41

36 characters successfully written to the file

Enter the details of student 5

Enter name of the student: King
Enter roll no: 9
Enter marks: 59

37 characters successfully written to the file

仕組み:

6 行目では、構造体ポインタ変数 fp が struct FILE 型で宣言されています。

7 行目では、サイズ 50 の文字配列 name が宣言されています。

8 行目では、int 型の roll_no、chars、i、n の 4 つの変数が宣言されています。

9 行目では、float 型の変数 Marks が宣言されています。

11 行目では、「records.txt」と「w」という 2 つの引数を指定して fopen() 関数が呼び出されます。成功すると、ファイル records.txt へのポインターを返し、ファイル records.txt を書き込み専用モードで開きます。失敗すると、NULL を返します。

13 ~ 17 行目では、if ステートメントを使用して fp の値をテストしています。 NULL の場合、printf() ステートメントはエラー メッセージを出力し、プログラムは終了します。それ以外の場合、プログラムは if ステートメントに続くステートメントから続行します。

19 行目の printf() ステートメントは、「Testing fprintf() function:\n\n」をコンソールに出力します。

行 21 ~ 22 で、プログラムはユーザーに、レコードを入力したい学生の数を入力するよう求めます。

24 行目から 41 行目では、for ループがユーザーに name、roll_no、およびそれぞれの生徒のマークの 3 つの情報を入力するよう求めています。 26 行目では、標準入力の内容をフラッシュ (削除) しています。この行は必要です。それ以外の場合は、30 行目の gets() 関数が改行文字 (生徒の数を尋ねている間に入力された) を読み取り、ユーザーを待ちません。生徒の名前を入力します。

38 行目では、fprintf() 関数が 5 つの引数とともに呼び出され、フォーマットされたデータがファイルに書き込まれます。データがファイルに正常に書き込まれた場合、ファイルに書き込まれた文字数が返され、変数 chars に割り当てられます。 40 行目の printf() ステートメントは、以前の fprintf() 関数の呼び出しによってファイルに書き込まれた文字の総数を出力します。ループは、i

43 行目で、fclose() 関数はファイルを閉じます。