一些初步事项。 main() 的函数签名应为 int main(void)、int main(int argc, char **argv) 或 int main(int argc, char *argv[])。
当您打开文件时,总是需要检查是否失败。 fopen() 失败时返回一个空指针,因此您可以检查这一点,并适当地处理错误。
在循环读取文件内容时,使用feof() 控制循环几乎总是错误的。这是因为feof() 检查文件结束指针,该指针本身仅在 IO 操作失败时设置。在贴出代码的情况下,进入循环,如果没有什么可读取的,singleLine缓冲区内容保持不变,将文本文件的最后一行加倍。
解决您的问题的最简单方法是假定每行包含 12 个等级,并在检索到的每行输入时使用sscanf() 对其进行解析。这不是一个优雅的解决方案:
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENTS 100
int main(void)
{
/* Open a file */
FILE *fPointer = fopen("hello.xls", "r");
/* Make sure file opened successfully */
if (fPointer == NULL) {
fprintf(stderr, "Unable to open file\n");
exit(EXIT_FAILURE);
}
char singleLine[500];
double studentGPA[MAX_STUDENTS];
int grade[12];
size_t studentIndex = 0;
/* Loop to fetch data and calculate GPAs */
while ((fgets(singleLine, sizeof singleLine, fPointer)) != NULL &&
studentIndex < MAX_STUDENTS) {
if (sscanf(singleLine, "%d %d %d %d %d %d %d %d %d %d %d %d",
&grade[0], &grade[1], &grade[2], &grade[3],
&grade[4], &grade[5], &grade[6], &grade[7],
&grade[8], &grade[9], &grade[10], &grade[11]) != 12) {
fprintf(stderr, "Incorrect number of grades on line %zu\n",
studentIndex);
exit(EXIT_FAILURE);
}
int sum = 0;
for (size_t i = 0; i < 12; i++) {
sum += grade[i];
}
studentGPA[studentIndex] = sum / 12.0;
++studentIndex;
}
/* Display GPAs */
for (size_t i = 0; i < studentIndex; i++) {
printf("Student %zu GPA: %.2f\n", i, studentGPA[i]);
}
return 0;
}
注意检查fgets()的返回值,控制文件读取循环;当返回空指针或达到最大学生数时,循环终止。此代码允许超过 10 名学生,但要求每个学生 12 年级。如果一行学生成绩不包含 12 个条目,则程序会打印一条错误消息并退出。这是通过检查来自sscanf() 的返回值来完成的,这是成功转换的次数。读取每个学生的成绩后,计算平均值并将其存储在studentGPAs 的数组中。
按照问题帖子中的建议,我假设了整数成绩条目,但 GPA 是使用浮点值计算的。另请注意,我假设成绩由空格分隔。如果成绩由逗号(或其他分隔符)分隔,则需要相应地修改格式字符串。
一种更优雅的方法是使用strtok() 根据一串分隔符将成绩线分成单独的标记。这些定界符delims = " ,\r\n" 表示strtok() 应该将行分成出现空格、逗号或换行符的标记。如果需要,可以简单地将其他分隔符添加到字符串中。
获取每行成绩,并首次调用strtok()。如果有令牌,则由atoi() 转换为int,并添加到sum。 numGrades 被更新,strtok() 被再次调用以获得新令牌。从一行读取所有成绩后,计算平均值并将其存储在studentGPA 数组中。请注意,平均计算中的分母乘以1.0 以强制计算为double。另请注意,更健壮的代码将使用strtol() 而不是atoi(),检查转换是否有错误。
使用此版本,可以评估不超过 MAX_STUDENTS 的任意数量的学生,并具有不同的成绩。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
int main(void)
{
/* Open a file */
FILE *fPointer = fopen("hello.xls", "r");
/* Make sure file opened successfully */
if (fPointer == NULL) {
fprintf(stderr, "Unable to open file\n");
exit(EXIT_FAILURE);
}
char singleLine[500];
double studentGPA[MAX_STUDENTS];
size_t studentIndex = 0;
char *token;
char *delims = " ,\r\n";
/* Loop to fetch data and calculate GPAs */
while ((fgets(singleLine, sizeof singleLine, fPointer)) != NULL &&
studentIndex < MAX_STUDENTS) {
int sum = 0;
int numGrades = 0;
/* Get grade tokens */
token = strtok(singleLine, delims);
while (token) {
sum += atoi(token);
++numGrades;
token = strtok(NULL, delims);
}
/* Calculate student GPA */
studentGPA[studentIndex] = sum / (1.0 * numGrades);
++studentIndex;
}
/* Display GPAs */
for (size_t i = 0; i < studentIndex; i++) {
printf("Student %zu GPA: %.2f\n", i, studentGPA[i]);
}
return 0;
}
这是一个示例hello.xls 文件,带有混合的逗号和空格分隔符。除非删除逗号,否则第一个程序将无法使用此文件。
3 4 2 3 3 4 3 2 3 3 4 4
2 2 3 4 2 3 3 2 3 2 2 3
1 2 3, 3 2 3 3, 3, 4 2 3 3
3 2 2 3 3 2 1 2 2 2 1 1
4 4 3 4 4 4 4 3 4 4 4 4
4, 3, 4, 4, 4, 4, 4, 3, 4, 4, 4, 4
2 3 2 2 2 1 2 2 2 3 2 2
3 4 4 4 4 4 4 4 4 4 4 4
3 2 3 3 2 3 3 3 2 3 3 3
4 1 4 3 4 4 4 2 4 4 4 4
这是带有这个成绩文件的第二个程序的输出:
Student 0 GPA: 3.17
Student 1 GPA: 2.58
Student 2 GPA: 2.67
Student 3 GPA: 2.00
Student 4 GPA: 3.83
Student 5 GPA: 3.83
Student 6 GPA: 2.08
Student 7 GPA: 3.92
Student 8 GPA: 2.75
Student 9 GPA: 3.50