Note, the OP wants to use fscanf. The names are white space separated (mix of space and newline), not strictly newline separated. fgets would read all 3 names into one men[i].name.
The first, little problem is that you simply don't print a space character between names. Just change the printf to "%s " (note the space after %s). That will at least make it a bit more legible.
The second problem is due to you using nested loops. A few helpful printf statements to debug:
Code:
int count = 0;
for (i=0; i< num_events; i++)
{
int j;
for (j=0; j< num_pairs; j++)
{
printf("i = %d, j = %d, loop iterations so far = %d\n", i, j, count);
fscanf(ifp,"%s", men[j].name);
printf("%s ", men[j].name);
printf("scanned into men[%d].name\n", j);
count++;
}
}
And the new output of your program:
Code:
i = 0, j = 0, loop iterations so far = 0
Quincy scanned into men[0].name
i = 0, j = 1, loop iterations so far = 1
Richard scanned into men[1].name
i = 0, j = 2, loop iterations so far = 2
Steven scanned into men[2].name
i = 1, j = 0, loop iterations so far = 3
Donna scanned into men[0].name
i = 1, j = 1, loop iterations so far = 4
Elizabeth scanned into men[1].name
i = 1, j = 2, loop iterations so far = 5
Francine scanned into men[2].name
Notice that j repeats going from 0 to 2 twice. The first time it reads the men's names, the second time the women's, which overwrites the men's names. That is a total of 6 iterations, in which you reading and print all of the names. Structure it like your malloc loops, two separate loops, not nested, one for men, one for women.
Code:
// no outer loop, one loop for men
for i from 0; < num_pairs
fscanf into men[i].name
// one loop for women
for i from 0; < num_pairs
fscanf into women[i].name