我正在尝试编写一个函数,从 txt 文件导入数据并将它们存储在结构数组中。
txt 文件如下所示:
1040,2024,2,29,13,0
1112,2027,3,12,13,0
1048,2026,6,20,10,30
1080,2024,2,29,10,30
1104,2026,6,9,14,0
1088,2024,2,29,11,30
1104,2024,2,29,14,0
1112,2024,2,29,12,0
1056,2025,2,28,11,30
1024,2024,2,29,10,0
1032,2024,2,29,13,30
1120,2024,2,29,11,0
1112,2027,3,10,11,0
1128,2024,2,29,12,30
1024,2027,3,12,10,30
但是当我尝试使用 sscanf 时,它们只是读为 0...这是我的函数:
int importNumbers(const char* datafile, struct Appointment appoints[], int max) {
int Num = 0;
FILE* fp= fopen(datafile, "r");
if (fp != NULL)
{
char line[100];
while (Num < max && fgets(line, sizeof(line), fp) != NULL) {
sscanf(&appNum, "%d,%d,%d,%d,%d,%d",
&appoints[Num].patientNumber,
&appoints[Num].date.year,
&appoints[Num].date.month,
&appoints[Num].date.year,
&appoints[Num].time.hour,
&appoints[Num].time.min);
Num++;
}
fclose(fp);
}
return Num;
}
警告:如果没有更多[发布]代码,我必须综合
struct
定义。
并且,来自我的最高评论:
你这样做
但它没有定义。您的意思是:&AppNum
?line
此外,您还使用
date.year
两次。第二个应该是date.day
更正此问题并添加打印代码,这是更正后的代码:
#include <stdio.h>
struct date {
int year;
int month;
int day;
};
struct time {
int hour;
int min;
};
struct Appointment {
int patientNumber;
struct date date;
struct time time;
};
struct Appointment apptlist[50];
int
importNumbers(const char *datafile, struct Appointment appoints[], int max)
{
int Num = 0;
FILE *fp = fopen(datafile, "r");
if (fp != NULL) {
char line[100];
while (Num < max && fgets(line, sizeof(line), fp) != NULL) {
#if 0
sscanf(&appNum,
#else
sscanf(line,
#endif
"%d,%d,%d,%d,%d,%d",
&appoints[Num].patientNumber,
&appoints[Num].date.year,
&appoints[Num].date.month,
#if 0
&appoints[Num].date.year,
#else
&appoints[Num].date.day,
#endif
&appoints[Num].time.hour,
&appoints[Num].time.min);
Num++;
}
fclose(fp);
}
return Num;
}
#define PRT(_sym) \
printf(" " #_sym "=%d",appt->_sym)
int
main(void)
{
int tot = importNumbers("data.txt",
apptlist,sizeof(apptlist) / sizeof(apptlist[0]));
printf("tot=%d\n",tot);
for (struct Appointment *appt = apptlist; appt < &apptlist[tot]; ++appt) {
PRT(patientNumber);
PRT(date.year);
PRT(date.month);
PRT(date.day);
PRT(time.hour);
PRT(time.min);
printf("\n");
}
return 0;
}
在上面的代码中,我使用
cpp
条件来表示旧代码与新代码:
#if 0
// old code
#else
// new code
#endif
#if 1
// new code
#endif
注意:这可以通过运行文件来清理
unifdef -k
对于给定的数据文件输入,以下是程序输出:
tot=15
patientNumber=1040 date.year=2024 date.month=2 date.day=29 time.hour=13 time.min=0
patientNumber=1112 date.year=2027 date.month=3 date.day=12 time.hour=13 time.min=0
patientNumber=1048 date.year=2026 date.month=6 date.day=20 time.hour=10 time.min=30
patientNumber=1080 date.year=2024 date.month=2 date.day=29 time.hour=10 time.min=30
patientNumber=1104 date.year=2026 date.month=6 date.day=9 time.hour=14 time.min=0
patientNumber=1088 date.year=2024 date.month=2 date.day=29 time.hour=11 time.min=30
patientNumber=1104 date.year=2024 date.month=2 date.day=29 time.hour=14 time.min=0
patientNumber=1112 date.year=2024 date.month=2 date.day=29 time.hour=12 time.min=0
patientNumber=1056 date.year=2025 date.month=2 date.day=28 time.hour=11 time.min=30
patientNumber=1024 date.year=2024 date.month=2 date.day=29 time.hour=10 time.min=0
patientNumber=1032 date.year=2024 date.month=2 date.day=29 time.hour=13 time.min=30
patientNumber=1120 date.year=2024 date.month=2 date.day=29 time.hour=11 time.min=0
patientNumber=1112 date.year=2027 date.month=3 date.day=10 time.hour=11 time.min=0
patientNumber=1128 date.year=2024 date.month=2 date.day=29 time.hour=12 time.min=30
patientNumber=1024 date.year=2027 date.month=3 date.day=12 time.hour=10 time.min=30