Hello my dears,
I have a problem regarding to reading from a file.
The problem is that I have 4 lines in the text file, the first is the IDs of employees, and the second line is their names, so I want to read it and sort it inside an array struct, but this attached code is for the first two lines, because the code works for the last two lines perfectly. I watched many concepts of reading from files and about fgets but I didn't find anything useful.
Code:
FILE*in; char tempNames[8];
char *namesLine;
int i;
in=fopen("employees.txt", "r");
employee_record AR[8];
for (i=0; i<8; i++){
fscanf(in, "%d", &AR[i].e_num);
}fgets(tempNames, 8, in);
namesLine=strtok(tempNames, " ,;");
i=0;
while (namesLine!=NULL){
strcpy(AR[i++].name, namesLine);
namesLine=strtok(NULL, " ,;");
}
for (i=0; i<8; i++)
printf("%d %s\n", AR[i].e_num, AR[i].name);
File's data:
111111 222222 333333 444444 555555 666666 777777 888888
Sami;Mohammad Kamal,Waleed Maryam;Bayan Ahmad,Jaber
That what I get when I run the compiler:
111111
222222 └i
333333 └i
444444 ╝>╜┘
555555 Ç
666666 ╠i
777777 πnàvΦ>╜┘α@
888888 ╠ a