【发布时间】:2013-11-09 06:43:40
【问题描述】:
我正在为我的 C 编程课做一个练习题,它告诉我编写一个从文件中读取变量的程序。在第一行,它应该读入一个整数 N。
从那里,它应该读取一个整数,然后每行有五个浮点数,共 N 行。它应该计算文件中所有浮点数的总和并将其写入另一个文件。
我编写了一个程序,该程序应该使用 fgets 将一行复制到一个字符串,并使用 sscanf 对其进行剖析并将段分配到不同的数组位置。但是,我在通过 sscanf 获取无关信息时遇到了一些问题(可能是空值或换行符)。它没有正确存储整数 N(它会产生大的随机值并通过无限循环创建运行时错误),并且它可能也无法在循环内部工作。
我怎样才能完善它以使其正确读取整数并浮动?
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING 30
#define MAX_LINE_SIZE 200
int main(void)
{
FILE *f1, *f2;
char filename[MAX_STRING];
char fileline[MAX_LINE_SIZE];
int N, eN;
float totalwage = 0;
int* ssn;
float** wage;
printf ("Enter a file name for data analysis: ");
scanf ("%s", &filename); //get file name from user input for reading
f1 = fopen (filename, "r");
fgets (fileline, MAX_LINE_SIZE, f1); //read first line
sscanf (fileline, "%d", &N); //pull integer from first line to determine how many lines follow
for (eN = 0; eN < N; eN ++) //read N lines following the first
{
// VVV read single line from file
fgets (fileline, MAX_LINE_SIZE, f1);
// VVV record data from line
sscanf (fileline, "%d, %f, %f, %f, %f, %f", &ssn[eN], &wage[eN][0], &wage[eN][1], &wage[eN][2], &wage[eN][3], &wage[eN][4]);
// VVV add the 5 wages on each line to a total
totalwage += wage[eN][0] + wage[eN][1] + wage[eN][2] + wage[eN][3] + wage[eN][4];
}
fclose (f1);
printf ("Enter a file name for the result: ");
scanf ("%s", &filename); //get file name from user input for writing
f2 = fopen (filename, "w");
fprintf (f2, "%f", totalwage); //store total of wages in file specified by user
printf ("\nThe information has been stored. Press any key to exit.\n");
getchar();
}
正在读取的文件是'wages.txt',其内容如下:
10
1, 10, 20, 30, 40, 50
2, 11, 12, 13, 14, 15
3, 21, 23, 25, 27, 29
4, 1, 2, 3, 4, 5
5, 30, 60, 90, 120, 150
6, 37, 38, 39, 40, 41
7, 40, 50, 60, 70, 80
8, 5, 10, 15, 20, 25
9, 80, 90, 100, 110, 120
10, 1000, 2000, 3000, 4000, 2000
回顾一下,问题在于存在运行时错误,其中程序由于某种无限循环而崩溃。通过一些调试,我看到它在第一行中没有正确读取为整数。它存储的不是值 10,而是像读取空字符一样存储大值。
我已添加代码以尝试为 ssn 和工资分配内存。但是,我不确定它是否正确完成,并且程序仍然存在崩溃的运行时错误。
ssn = malloc (N*MAX_STRING);
wage = malloc (N*MAX_STRING);
for (eN = 0; eN < N; eN ++)
{
wage[eN] = malloc (N*MAX_STRING);
}
【问题讨论】:
-
"C - 使用 sscanf 读取多个整数和浮点数" - 不,使用
fgets()和strtol()和strtod()。此外,scanf ("%s", &filename);是基于类型不匹配的 UB。涉及wage指针的长sscanf()行也是UB,因为该指针不指向任何地方,它是未初始化的。 -
这些库调用中的 any 没有一个返回值检查。你想知道可能出了什么问题?
-
我已经成功为ssn和工资分配了内存。我仍然从 sscanf 获得不可预测的值。
-
如果你除了对工资求和之外什么都不做,那么你不需要一个足够大的数组来保存所有的工资。您可能需要一个大到足以容纳一行工资的数组(因此,对
sscanf()的一次调用会将数据读入数组,然后将数组相加到总数中。您甚至可以编写代码,这样您只需要一个用于读取值的float变量,加上一个用于保存工资总和的变量,尽管这有点困难,而且可能不值得努力。
标签: c file integer fgets scanf