I am reading a line from a file containing the names of people, first line contains names of males, and seconds line contains names of females. Then I want to store these names in two arrays, one for males, one for females, however when I print them I get weird things. I'm not sure if I am not reading them correctly, or printing them incorrectly
char line[100]; //holds line read
char *item; //item in a line
char *item2;
int participants = 5; //number of people in the event
char* maleNames[participants];
char* femaleNames[participants];
fgets(line, 255, file);
int i;
item = strtok(line, " ");
for(i=0; i<participants; i++)
{
maleNames[i] = item;
item = strtok(NULL, " ");
}
//read female names now
fgets(line, 1024, file);
item2 = strtok(line, " ");
for(i=0; i<participants; i++)
{
femaleNames[i] = item2;
item2 = strtok(NULL, " ");
}
These lines are read
John Jeffrey Adam Mark Peter
Jenny Alice Sally Wendy Amanda
However when I print them out like this:
for(i=0;i<participants;i++)
{
printf("%s %s\n", maleNames[i], femaleNames[i]);
}
I get something so different:
Jenny Jenny
Alice
ally Sally
Wendy Wendy
Amanda
Note: if I print the names of males right after they're read before reading the female names, then they are printed correctly