Hello. I need to open a file and sort its contents by structure. (Base structure) The file contains something like this: Ivan Ivanov 13122016 79811111111 Its contents are parsed into an array of type char (Collection). The data is separated by spaces (''), and the lines are '\ n'. In general, my algorithm does not work. (the characters are crooked, but are saved in the structure, but the numbers are not) And I could not fix it. Can you help fix it? Thank. Code:

char Collection[200]; char Temp[2]; Temp[1] = '\0'; for (int i = 0; i < 200; i++) { z++; if (Collection[i] == '\n' || Collection[i] == '\0') break; if (Collection[i] == ' ') { start = i; for (int j = start + 1; j <= 200; ++j) { if (Collection[j] == ' ') { end = j; break; } } for (int j = start + 1; j <= end - 1; ++j) { if (z == 0) Base[z].Name[j] = Collection[j]; if (z == 1) Base[z].Surname[j] = Collection[j]; if (z == 2) { Temp[1] = Collection[j]; if (end - j - 1 != 0) { Digit = Digit + atoi(Temp) * 10 ^ (end - j - 1); printf("%d", Digit); } else { Digit = Digit + atoi(Temp); Base[0].Date = Digit; } } } } } 

    1 answer 1

    Use scanf and do not suffer ... For example:

     struct Data { char name[50]; char surname[50]; int date; long long num; }; int main(int argc, const char * argv[]) { char s[] = "Ivan Ivanov 13122016 79811111111"; struct Data d; sscanf(s,"%49s %49s %d %lld",d.name,d.surname,&d.date,&d.num); printf("%s %s - %d -- %lld\n",d.name,d.surname,d.date,d.num); } 

    You have not described the structure, so I wrote mine.