C语言读取预约数据文件失败:代码报错求助
C语言文件读取问题排查
问题描述
编写了一段C语言代码,意图读取appointmentData.txt文本文件中的预约数据并存储到对应变量,但代码无法读取文件。已排查文件路径、拼写等问题仍未解决,附上代码及待读取文本内容,请求指出错误。
我的代码
#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", 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
错误分析与修复方案
1. 缺失结构体定义
代码中使用了struct Appointment、appoints[i].date、appoints[i].time,但未定义这些结构体,编译器无法识别类型,直接编译失败。需补充定义:
// 日期结构体 struct Date { int year; int month; int day; }; // 时间结构体 struct Time { int hour; int min; }; // 预约结构体 struct Appointment { int patientNumber; struct Date date; struct Time time; };
将这段代码放在#define MAX_APPOINTMENTS 50之后、main函数之前。
2. 未声明appoints变量
main函数中直接使用appoints作为参数,但未定义该变量。需在main内添加:
struct Appointment appoints[MAX_APPOINTMENTS];
3. 缺失函数声明
importAppointments函数在main之后定义,main调用前未声明,会导致编译器参数类型匹配错误。需在main前添加函数声明:
int importAppointments(char *filename, struct Appointment appoints[], int max);
4. fscanf格式串的潜在问题
原格式串%d,%d,%d,%d,%d,%d%c依赖最后一个字符是换行符,若文件最后一行无换行符会读取失败。更稳妥的方式是读取6个整数后跳过剩余行内容:
while (i < max && fscanf(file, "%d,%d,%d,%d,%d,%d", &appoints[i].patientNumber, &appoints[i].date.year, &appoints[i].date.month, &appoints[i].date.day, &appoints[i].time.hour, &appoints[i].time.min) == 6) { // 跳过当前行剩余字符(含换行) int c; while ((c = fgetc(file)) != '\n' && c != EOF); i++; }
5. 文件打开失败的返回值歧义
原代码打开失败返回0,与读取0条记录的返回值混淆,可改为返回-1标识打开失败,main中做判断:
// importAppointments错误分支 printf("Error opening file: %s\n", filename); return -1; // main中调用逻辑 int appointmentCount = importAppointments("appointmentData.txt", appoints, MAX_APPOINTMENTS); if (appointmentCount == -1) { return 1; }
修复后的完整代码
#include <stdio.h> #include <stdlib.h> #define MAX_APPOINTMENTS 50 // 定义日期结构体 struct Date { int year; int month; int day; }; // 定义时间结构体 struct Time { int hour; int min; }; // 定义预约结构体 struct Appointment { int patientNumber; struct Date date; struct Time time; }; // 函数声明 int importAppointments(char *filename, struct Appointment appoints[], int max); int main(){ struct Appointment appoints[MAX_APPOINTMENTS]; int appointmentCount = importAppointments("appointmentData.txt", appoints, MAX_APPOINTMENTS); if (appointmentCount == -1) { return 1; } printf("Imported %d appointment records...\n", appointmentCount); return 0; } int importAppointments(char *filename, struct Appointment appoints[], int max){ FILE* file = fopen(filename, "r"); int i=0; if (file != NULL) { while (i < max && fscanf(file, "%d,%d,%d,%d,%d,%d", &appoints[i].patientNumber, &appoints[i].date.year, &appoints[i].date.month, &appoints[i].date.day, &appoints[i].time.hour, &appoints[i].time.min) == 6) { // 跳过当前行剩余字符 int c; while ((c = fgetc(file)) != '\n' && c != EOF); i++; } fclose(file); }else{ printf("Error opening file: %s\n", filename); return -1; } return i; }
内容的提问来源于stack exchange,提问作者awnia
相关产品推荐
相关产品推荐

