【发布时间】:2016-04-11 18:35:15
【问题描述】:
***用新代码更新 我正在尝试使用未知数量的数据行加载文件数据。 数据组织为 last, first:score1:score2:score3:score5:lettergrade
我正在将文件(某种程度上)加载到数组中并打印。 打印数据时,它不打印名字,只打印第一个。除了最后一个数据元素外,它为所有数据元素打印相同的整数,结果为 0。此外,它不会刷新不返回 8 个元素的数据。 请帮忙! 提前致谢。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FFLUSH while(fgetsc(fp) != '\n')
#define MAX_STUDENTS 45
typedef struct aStudent
{
char name[26];
int id[7];
int test1[4];
int test2[4];
int proj1[4];
int proj2[4];
int proj3[4];
char grade[4];
} aStudent;
int main(void)
{
char file_name[FILENAME_MAX];
FILE* fp;
int i;
int rc;
int dc;
aStudent studentArray[MAX_STUDENTS];
printf("Enter File Name: ");
rc = scanf("%s", &file_name);
if (rc == 0)
{
printf("\n\nError: No file name entered.");
exit(0);
}
fp = fopen(file_name, "r");
if (fp == NULL)
{
printf("Error: Could not open file %s for read", file_name);
exit(0);
}
for(int i=0; i< 45; i++)
{
dc = (fscanf(fp, "%s[^:]%d[^:]%d[^:]%d[^:]%d[^:]%d[^:]%d[^:]%f[^:]%c[^:\n]", studentArray[i].name, studentArray[i].id, studentArray[i].test1, studentArray[i].test2, studentArray[i].proj1, studentArray[i].proj2, studentArray[i].proj3, studentArray[i].grade));
if (dc != 8)
{
FFLUSH;
}
printf("%s %d %d %d %d %d %d %.2f %c", studentArray[i].name, studentArray[i].id, studentArray[i].test1, studentArray[i].test2, studentArray[i].proj1, studentArray[i].proj2, studentArray[i].proj3, studentArray[i].grade);
}
return 0;
}
【问题讨论】:
-
“令牌前的预期表达式”错误。好的。哪一个。线。是。这。错误。开。
-
FFLUSH宏在您使用它的末尾需要一个分号 -
抱歉,第 53 行。在 FFLUSH 下仅包含 } 的行。
-
并非如此。该宏需要死亡。摆脱它。做一个适当的功能。
-
谢谢矢野!我还在第 48 行收到警告,说我的 fgets 从不兼容的指针类型传递参数。我的任务对我来说似乎是正确的?