geekdoc-python-zh/docs/overiq/084.md

4.4 KiB
Raw Permalink Blame History

C 语言中的fprintf()函数

原文:https://overiq.com/c-programming-101/fprintf-function-in-c/

最后更新于 2020 年 7 月 27 日


格式化文件输入和输出

至此,我们已经看到了如何读写文件中的字符和字符串。在现实世界中,数据由许多不同的类型组成。在本章中,我们将学习如何以格式化的方式输入和输出不同类型的数据。当我们想要以特定格式读取或写入数据时,我们使用格式化的输入和输出。

fprintf()函数

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

fprintf()功能与printf()相同,但它不是将数据写入控制台,而是将格式化数据写入文件。fprintf()函数的几乎所有参数都与printf()函数相同,除了它有一个额外的参数,该参数是指向格式化输出将被写入的文件的文件指针。成功后,它返回写入文件的字符总数。出错时,返回EOF

下面的程序演示了如何使用fprintf()功能。

#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;
}

预期输出:

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 的字符名称数组。

在第 8 行中,声明了四个变量,即 roll_no、chars、I 和 int 类型的 n。

在第 9 行中,声明了 float 类型的变量标记。

在第 11 行fopen()函数用两个参数调用即“records.txt”和“w”。成功后它返回一个指向 file records.txt 的指针,并以只写模式打开 file records.txt。失败时它返回空值。

在第 13-17 行if 语句用于测试 fp 的值。如果为空printf()语句将打印错误消息,程序终止。否则,程序继续执行 if 语句后面的语句。

在第 19 行printf()语句将“Testing fprintf()函数:\n\n”打印到控制台。

在第 21-22 行,程序要求用户输入他想输入其记录的学生人数。

在第 24-41 行for 循环要求用户输入三条信息名称、roll_no 和相应学生的标记。在第 26 行中,我们正在刷新(删除)标准输入的内容,这一行是必要的,否则第 30 行中的 get()函数将读取换行符(在询问学生人数时输入),并且不会等待用户输入学生的姓名。

在第 38 行,调用 fprintf()函数和 5 个参数,将格式化数据写入文件。如果数据已成功写入文件,它将返回写入文件的字符数,然后将其分配给可变字符。在第 40 行中printf()语句打印由 fprintf()函数的前一次调用写入文件的字符总数。循环将继续要求更多的学生记录,直到 i < n。一旦 n 变得大于 I控制就从 for 循环中出来。

在第 43 行fclose()函数关闭文件。