[应用方案] fscanf()函数

[复制链接]
 楼主| 10299823 发表于 2025-7-21 15:29 | 显示全部楼层 |阅读模式
1 函数原型

fscanf():从指定流stream读取格式化数据,函数原型如下:

  1. int fscanf ( FILE * stream, const char * format, ... );

cstdio库描述如下:

  1. Read formatted data from stream
  2. 1. Reads data from the stream and stores them according to the parameter format into the locations pointed by the additional arguments.
  3. 2. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.

2 参数

fscanf()函数参数包括三部分:

  • 参数stream是fscanf()函数要读取的流,类型为FILE*;stream可以是文件流或标准输入流;当是文件流时,stream就是fopen()函数的返回值;当是标准输入流时,stream就是stdin;
  • format :格式字符串,与scanf()函数一致;
  • … : 可变数量的参数,与scanf()函数一致。
3 返回值

fscanf()函数返回值类型为int型:

  • 读取成功,则返回成功读取的项数;
  • 读取失败,则返回EOF。

cstdio库描述如下:

  1. 1. On success, the function returns the number of items of the argument list successfully filled.
  2. 2. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
  3. 3. If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

fscanf()函数,预期读取m项,成功读取n项:

[td]
读取比较读取状况读取结果返回值状态设置
n=m全部成功读取读取成功返回nNA
0<n<m部分成功读取,遇到匹配失败读取成功返回nNA
0<n<m部分成功读取,遇到读取错误读取成功返回n设置错误指示符(ferror)
0<n<m部分成功读取,遇到文件末尾读取成功返回n设置文件结束指示符(feof)
n=0未成功读取任何项,遇到匹配失败读取成功返回nNA
n=0未成功读取任何项,遇到读取错误读取失败返回EOF设置错误指示符(ferror)
n=0未成功读取任何项,遇到文件末尾读取失败返回EOF设置文件结束指示符(feof)
4 比较

fscanf()函数和scanf()函数的工作原理类似,差异如下:

  • fscanf()函数从指定流stream中读取格式化数据;scanf()函数从标准输入流stdin中读取格式化数据;
  • 将fscanf()函数的参数stream指定为stdin,则fscanf()函数的功能和scanf()函数的功能完全相同。
5 示例5.1 示例1

使用fscanf()函数读文件并打印,示例代码如下所示:

  1. int main()
  2. {
  3.    //
  4.    float floatData = 0.0f;

  5.    FILE* fp = NULL;
  6.    //
  7.    if ((fp = fopen("1.txt", "r")) == NULL)
  8.    {
  9.       perror("Failed to open file ");
  10.       exit(1);
  11.    }
  12.    //
  13.    printf("scanf()函数读文件:\n");
  14.    while (fscanf(fp, " %f", &floatData) != EOF)
  15.    {
  16.       printf("floatData = %f\n", floatData);
  17.    }
  18.    //
  19.    fclose(fp);
  20.    //
  21.    printf("\n");
  22.    //
  23.    printf("scanf()函数读stdin:\n");
  24.    while (fscanf(stdin, " %f", &floatData) != 0)
  25.    {
  26.       printf("floatData = %f\n", floatData);
  27.    }
  28.    //
  29.    return 0;
  30. }

文件内容如下图所示:

代码运行结果如下图所示:

5.2 示例2

检查fscanf()函数成功读取的项数,示例代码如下所示:

  1. int main()
  2. {
  3.    //
  4.    FILE* fp = NULL;
  5.    int intData1 = 0;
  6.    int intData2 = 0;
  7.    int intData3 = 0;
  8.    //
  9.    fp = fopen("1.txt", "r");

  10.    if (fp == NULL) {
  11.       perror("Failed to open file ");
  12.       exit(1);
  13.    }
  14.    //
  15.    int rdItem = fscanf(fp, " %d %d %d", &intData1, &intData2, &intData3);
  16.    printf("intData1 = %d\n", intData1);
  17.    printf("intData2 = %d\n", intData2);
  18.    printf("intData3 = %d\n", intData3);
  19.    printf("成功读取项数 :%d\n", rdItem);
  20.    //
  21.    fclose(fp);
  22.    //
  23.    return 0;
  24. }

当文件内容为3个整数,全部成功读取,代码运行结果如下图所示:

当文件内容为2个整数和1个字符串,部分成功读取,遇到匹配失败,代码运行结果如下图所示:

当文件内容3个字符串,未成功读取任何项,遇到匹配失败,代码运行结果如下图所示:

当文件内容为空,未成功读取任何项,遇到文件末尾,代码运行结果如下图所示:


deliahouse887 发表于 2025-8-7 12:16 | 显示全部楼层
工作方式类似于 scanf(),但 fscanf() 从指定的文件流读取输入,而不是从标准输入(stdin)。
juliestephen 发表于 2025-8-8 15:16 | 显示全部楼层
函数返回成功匹配和赋值的项目数,如果到达文件末尾或发生读取错误,则返回EOF。
zerorobert 发表于 2025-8-8 15:56 | 显示全部楼层
fscanf()函数是C语言标准库中的一个函数,用于从文件中读取格式化的数据。
pixhw 发表于 2025-8-10 21:23 | 显示全部楼层
直接操作文件流,避免逐字符读取,适合大数据量处理
sesefadou 发表于 2025-8-11 22:15 | 显示全部楼层
函数返回成功匹配和赋值的项目数,不包括空格和换行符。如果遇到输入结束(EOF),则返回 EOF。
 楼主| 10299823 发表于 2025-8-12 12:04 | 显示全部楼层
fscanf()函数的工作原理是根据format字符串中指定的格式,从stream指向的文件流中读取数据,并将读取的数据存储在由额外参数指定的内存位置。
uiint 发表于 2025-8-12 15:06 | 显示全部楼层
在使用fscanf()函数时,需要根据返回值进行错误处理
ulystronglll 发表于 2025-8-12 22:32 | 显示全部楼层
fscanf() 通过灵活的格式字符串实现了对文件数据的高效解析,但其行为受空白符处理、格式说明符和返回值影响的复杂规则制约。
wilhelmina2 发表于 2025-8-14 18:28 | 显示全部楼层
#include <stdio.h>

int main() {
    FILE *file;
    int integer;
    float floating_point;
   
    // 打开文件
    file = fopen("data.txt", "r");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }
   
    // 从文件中读取整数和浮点数
    fscanf(file, "%d %f", &integer, &floating_point);
   
    // 输出读取的数据
    printf("Integer: %d\n", integer);
    printf("Floating Point: %f\n", floating_point);
   
    // 关闭文件
    fclose(file);
   
    return 0;
}
wilhelmina2 发表于 2025-8-14 21:18 | 显示全部楼层
fscanf()函数是C语言中的一个输入函数,用于从文件中按照指定格式读取数据。
jkl21 发表于 2025-8-14 22:00 | 显示全部楼层
使用时注意格式匹配、缓冲区安全、返回值检查。
maudlu 发表于 2025-8-15 21:44 | 显示全部楼层
fscanf() 函数按照 format 字符串中指定的格式从 stream 指向的文件中读取数据,并将读取的数据存储在随后的参数中提供的地址中。
maudlu 发表于 2025-8-16 17:56 | 显示全部楼层
通过fscanf()函数,可以灵活地从文件中读取数据并进行处理,适用于需要解析有规律的文件内容的场景。
tifmill 发表于 2025-8-16 19:33 | 显示全部楼层
从文件流 stream 中按 format 指定的格式读取数据,并将结果存入后续参数指向的变量中
 楼主| 10299823 发表于 2025-8-16 21:01 | 显示全部楼层
fscanf()函数通常与fprintf()函数配合使用,后者用于向文件写入格式化的数据。
febgxu 发表于 2025-8-16 21:24 | 显示全部楼层
对于复杂输入,建议先用 fgets() 读取整行,再用 sscanf() 解析。
belindagraham 发表于 2025-8-18 20:10 | 显示全部楼层
务必检查 fscanf() 的返回值
hilahope 发表于 2025-8-18 20:31 | 显示全部楼层
为每个要读取的数据项提供一个对应的指针参数。
hearstnorman323 发表于 2025-8-18 21:40 | 显示全部楼层
fscanf() 是 C 语言标准库中的一个输入函数,用于从文件流(FILE *)中读取格式化的数据。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

56

主题

3628

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部