A long time ago I was entrusted with this work at my university which I ended up solving in a very precarious way, I was wondering how you would do to collect information from a file and save it in a structure composed of strings.
The fields of the structure are organized in different ways such that, for example, in the name field we have both the first and last names, separated by a space.
That caused a problem in the way I collected the information, since I counted by spaces to enter it in the structure, I solved it by scanning the two strings and then concatenating them.
An example of the file:
IWM14
aq201 Juan Lopez 555815964 3 FP 5.5 TP 7.5 TSO 5.9
aq197 Rosa Fernandez 555863564 3 FP 6.5 TP 8.5 AM 6.8
I was wondering how you would do to take a line of information, and classify it in the different fields of the structure, and so with all the lines of the file (omitting the header), then a linked list of structures is created, but that is a separate issue.
I add my solution:
TAlumn readData(FILE *fich)
{
Auxiliary aux; //Auxiliary Structure
TAlumn result;
int i;
char name[40];
fscanf(fich, "%s", result.number);
fscanf(fich, "%s", aux.name);
fscanf(fich, "%s", aux.lastname);
fscanf(fich, "%s", result.phone);
fscanf(fich, "%d", &result.subjectsnumber);
strcat(result.name, aux.name);
strcat(result.name, " ");
strcat(result.name, aux.surnames);
for (i = 0; i < result.subjectsnumber; i++)
{
fscanf(fich, "%s", result.subjects[i].subjectName);
fscanf(fich, "%f", &result.subjects[i].grade);
}
return result;
}
TAlumn structure definition:
typedef struct
{
char subjectName[MAX];
float grade;
} TEvaluation;
typedef struct
{
char number[N];
char name[MAX];
char phone[MAX];
int subjectsnumber;
TEvaluation subjects[N];
} TAlumn;
Thanks for the help and the time dedicated, I am a little new to the subject of files.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…