如何从我创建的文本文件中读取结构?
How to read a struct from a text file that I created?
我正在为模拟患者数据库的程序编写 C 代码。我设法编写了将患者添加到数据库并将信息保存到文本文件的代码。
关闭数据库并将信息保存到文本文件的代码在下面并且工作正常:
FILE *close_file;
close_file = fopen(file_name, "w");
if (close_file == NULL){
printf("save information to file failed!\n");
fclose(close_file);
exit(EXIT_FAILURE);
}
fprintf(close_file, "%d\n", *num_of_structs);/*Enters the number of the
patient in the DB in the
beginning of the text file*/
for (int j = 0; j < *num_of_structs;j++){
/*the for loop down below is to write referens number to the patients x-
ray pictures, so the first two numbers in the text file belong to an array
of two rooms*/
for (int picture = 0; picture < 2; picture++){
fprintf(close_file, "%d ", patientRegister[j].pictures[picture]);
}
/*down below I write the personal number and the name of the patient to
the text file*/
fprintf(close_file, "%d %s", patientRegister[j].personal_number,
patientRegister[j].patient_name);
}
fclose(close_file);
}
文本文件可能如下所示:
https://ibb.co/FKdx4vg
我的问题是如何读取这个文本文件?我尝试了很多代码但没有成功。请参阅我的代码,该代码对我不起作用,除了以下行:
fscanf(existing_file, "%d", num_of_structs);/*This reads the first number
at the top of the text file and which represents the number of patients in
the DB*/
查看应该打开文本文件并从中读取的整个函数:
void open_existing_file(Patient patientRegister[], char file_name[], int
*num_of_structs) {
FILE *existing_file;
existing_file = fopen(file_name, "r");
if (existing_file == NULL) {
printf("Open existing file failed\n");
fclose(existing_file);
exit(EXIT_FAILURE);
}
fscanf(existing_file, "%d", num_of_structs);/*This reads the first number
at the top of the text file and which represents the number of patients in
the DB*/
/*I guess I'm doing a lot of wrong code down here?!*/
for (int j = 0; j < (*num_of_structs); j++) {
for (int picture = 0; picture < 2; picture++) {
fscanf(existing_file, "%d", patientRegister[j].pictures[picture]);
}
fscanf(existing_file, "%d %s", patientRegister[j].personal_number,
patientRegister[j].patient_name);}
fclose(existing_file);
}
scanf
的 %d
格式说明符需要 int
的 地址 ,即 int *
,而不是 int *
int
。这是为了将读取的值写入变量。
所以你想要:
fscanf(existing_file, "%d", &num_of_structs);
...
fscanf(existing_file, "%d", &patientRegister[j].pictures[picture]);
...
fscanf(existing_file, "%d %s", &patientRegister[j].personal_number, patientRegister[j].patient_name);
请注意,%s
不需要 &
来读入数组,因为数组会自动衰减到指向其第一个元素的指针。
我正在为模拟患者数据库的程序编写 C 代码。我设法编写了将患者添加到数据库并将信息保存到文本文件的代码。
关闭数据库并将信息保存到文本文件的代码在下面并且工作正常:
FILE *close_file;
close_file = fopen(file_name, "w");
if (close_file == NULL){
printf("save information to file failed!\n");
fclose(close_file);
exit(EXIT_FAILURE);
}
fprintf(close_file, "%d\n", *num_of_structs);/*Enters the number of the
patient in the DB in the
beginning of the text file*/
for (int j = 0; j < *num_of_structs;j++){
/*the for loop down below is to write referens number to the patients x-
ray pictures, so the first two numbers in the text file belong to an array
of two rooms*/
for (int picture = 0; picture < 2; picture++){
fprintf(close_file, "%d ", patientRegister[j].pictures[picture]);
}
/*down below I write the personal number and the name of the patient to
the text file*/
fprintf(close_file, "%d %s", patientRegister[j].personal_number,
patientRegister[j].patient_name);
}
fclose(close_file);
}
文本文件可能如下所示: https://ibb.co/FKdx4vg
我的问题是如何读取这个文本文件?我尝试了很多代码但没有成功。请参阅我的代码,该代码对我不起作用,除了以下行:
fscanf(existing_file, "%d", num_of_structs);/*This reads the first number
at the top of the text file and which represents the number of patients in
the DB*/
查看应该打开文本文件并从中读取的整个函数:
void open_existing_file(Patient patientRegister[], char file_name[], int
*num_of_structs) {
FILE *existing_file;
existing_file = fopen(file_name, "r");
if (existing_file == NULL) {
printf("Open existing file failed\n");
fclose(existing_file);
exit(EXIT_FAILURE);
}
fscanf(existing_file, "%d", num_of_structs);/*This reads the first number
at the top of the text file and which represents the number of patients in
the DB*/
/*I guess I'm doing a lot of wrong code down here?!*/
for (int j = 0; j < (*num_of_structs); j++) {
for (int picture = 0; picture < 2; picture++) {
fscanf(existing_file, "%d", patientRegister[j].pictures[picture]);
}
fscanf(existing_file, "%d %s", patientRegister[j].personal_number,
patientRegister[j].patient_name);}
fclose(existing_file);
}
scanf
的 %d
格式说明符需要 int
的 地址 ,即 int *
,而不是 int *
int
。这是为了将读取的值写入变量。
所以你想要:
fscanf(existing_file, "%d", &num_of_structs);
...
fscanf(existing_file, "%d", &patientRegister[j].pictures[picture]);
...
fscanf(existing_file, "%d %s", &patientRegister[j].personal_number, patientRegister[j].patient_name);
请注意,%s
不需要 &
来读入数组,因为数组会自动衰减到指向其第一个元素的指针。