简体   繁体   English

如何从我创建的文本文件中读取结构?

[英]How to read a struct from a text file that I created?

I'm writing C code for a program to simulate a patient database. 我正在为程序编写C代码来模拟患者数据库。 I managed to write the code that add patient to the database and save the information to a text file. 我设法编写了将患者添加到数据库的代码,并将信息保存到文本文件中。

The code that close the DB and saves the information to text file is down below and it works fine: 关闭数据库并将信息保存到文本文件的代码如下所示,它可以正常工作:

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);

}

the text file can look like this: https://ibb.co/FKdx4vg 文本文件可能如下所示: https//ibb.co/FKdx4vg

My problem is how to read from this text file? 我的问题是如何从这个文本文件中读取? I tried a lot of code but without success. 我尝试了很多代码但没有成功。 See my code which doesn't work for me except the line: 看到我的代码除了行之外对我不起作用:

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*/

See the whole function that supposed to open a text file and read from it: 查看应该打开文本文件并从中读取的整个函数:

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);
}

The %d format specifier to scanf expects the address of an int , ie an int * , not an int . scanf%d格式说明符需要int地址 ,即int * ,而不是int This is so it take the value it read and write it to the variable. 这样它就会读取它所读取的值并将其写入变量。

So you want: 所以你要:

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);

Note that you don't need & for %s to read into an array, since an array automatically decays to a pointer to its first element. 请注意,您不需要& %s来读取数组,因为数组会自动衰减到指向其第一个元素的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM