这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX_APPOINTMENTS 50
int main(){
int appointmentCount = importAppointments("appointmentData.txt", appoints, MAX_APPOINTMENTS);
printf("Imported %d appointment records...\n\n", appointmentCount);
return 0;
}
int importAppointments(char *filename, struct Appointment appoints[], int max){
FILE* file = fopen(filename, "r");
int i=0;
char ch;
if (file != NULL)
{
while (i < max && fscanf(file, "%d,%d,%d,%d,%d,%d%c",
&appoints[i].patientNumber, &appoints[i].date.year, &appoints[i].date.month,
&appoints[i].date.day, &appoints[i].time.hour, &appoints[i].time.min, &ch) == 7)
{
if(ch != '\n')
{
while(fgetc(file) != '\n');
}
i++;
}
fclose(file);
file = NULL;
}else{
printf("Error opening file: %s\n", filename);
return 0;
}
return i;
}
我正在尝试阅读这篇文章:
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
我一直出错;代码无法读取文件;我已经完成了一些故障排除步骤,但无法找到问题所在。 我检查了文件是否在同一个目录中,检查了是否有错字等
main()
:您需要文件底部的 importAppointments()or move
main()` 原型。
main()
:appoints
未申报。
struct Appointment
没有申报
至于特定的错误消息,它表明您的文件
appointmentData.txt
在当前工作目录中不可读。您可以使用 getcwd()
找出那是什么。还要确保查看文件的所有者/组/权限。
#include <stdio.h>
#include <stdlib.h>
struct Appointment {
int patientNumber;
struct {
int year;
int month;
int day;
} date;
struct {
int hour;
int min;
} time;
};
#define MAX_APPOINTMENTS 50
int importAppointments(char *filename, struct Appointment appoints[], int max){
FILE *file = fopen(filename, "r");
if(!file) {
printf("Error opening file: %s\n", filename);
return 0;
}
int i=0;
char ch;
while (i < max && fscanf(file, "%d,%d,%d,%d,%d,%d%c",
&appoints[i].patientNumber, &appoints[i].date.year, &appoints[i].date.month,
&appoints[i].date.day, &appoints[i].time.hour, &appoints[i].time.min, &ch) == 7) {
if(ch != '\n')
while(fgetc(file) != '\n');
i++;
}
fclose(file);
return i;
}
void printAppointments(size_t n, struct Appointment appoints[n]) {
for(size_t i = 0; i < n; i++)
printf("patient number: %d\n"
"date (YYYY/MM/DD): %d/%d/%d\n"
"time: %d:%d\n"
"%s",
appoints[i].patientNumber,
appoints[i].date.year,
appoints[i].date.month,
appoints[i].date.day,
appoints[i].time.hour,
appoints[i].time.min,
i + 1 < n ? "-\n" : "\n"
);
}
int main(void) {
struct Appointment appoints[MAX_APPOINTMENTS];
int appointmentCount = importAppointments("appointmentData.txt", appoints, MAX_APPOINTMENTS);
printf("Imported %d appointment records...\n\n", appointmentCount);
printAppointments(appointmentCount, appoints);
}
和示例输出:
Imported 15 appointment records...
patient number: 1040
date (YYYY/MM/DD): 2024/2/29
time: 13:0
-
patient number: 1112
date (YYYY/MM/DD): 2027/3/12
time: 13:0
-
patient number: 1048
date (YYYY/MM/DD): 2026/6/20
time: 10:30
-
patient number: 1080
date (YYYY/MM/DD): 2024/2/29
time: 10:30
-
patient number: 1104
date (YYYY/MM/DD): 2026/6/9
time: 14:0
-
patient number: 1088
date (YYYY/MM/DD): 2024/2/29
time: 11:30
-
patient number: 1104
date (YYYY/MM/DD): 2024/2/29
time: 14:0
-
patient number: 1112
date (YYYY/MM/DD): 2024/2/29
time: 12:0
-
patient number: 1056
date (YYYY/MM/DD): 2025/2/28
time: 11:30
-
patient number: 1024
date (YYYY/MM/DD): 2024/2/29
time: 10:0
-
patient number: 1032
date (YYYY/MM/DD): 2024/2/29
time: 13:30
-
patient number: 1120
date (YYYY/MM/DD): 2024/2/29
time: 11:0
-
patient number: 1112
date (YYYY/MM/DD): 2027/3/10
time: 11:0
-
patient number: 1128
date (YYYY/MM/DD): 2024/2/29
time: 12:30
-
patient number: 1024
date (YYYY/MM/DD): 2027/3/12
time: 10:30