This is a school assignment that I got today and I can't figure out why I get messed up output. The file is supposed to have one line with several numbers and words that are to be put into separate variables, the last number in that line will determine how many more lines to input before beginning again. This is done until the end of the file is reached.

So if the first line is
10428 Davis Richard Mahomet iL 64855 1
then the program should read one line after that before moving on to the next element in the structure array.
If the first line is
10186 Allan James Gainseville WA 21813 4
then the program will read four lines before going to the next element in the structure array.

In the process of debugging, I found that when I create a new data file with only the first lines instead of everything, the program will read them fine. But when I add the other lines I get messed up output.

Here's the source code, let me know if anyone wants the datafile as well to see what I mean.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXNUMBER 5
#define LINELENGTH 26

typedef struct {
  long int number1;
  char string1[16];
  char string2[16];
  char string3[16];
  char string4[3];
  char line1[26];
  char line2[26];
  char line3[26];
  char line4[26];
  long int number2;
  int number3;
} INPUT;

int main( void ) {
  int i = 0, j = 0;
  FILE *INSTREAM;
  INPUT data[5];

  if ( ( INSTREAM = fopen("a:\\datafile.txt", "r") ) == NULL ) {
    printf("Could not open file!");
	exit(1);
  }

  while( i < MAXNUMBER ) {
	for ( j = 0; j < MAXNUMBER; j++ ) {
	  data[i].string1[j] = '\0';
	  data[i].string2[j] = '\0';
	  data[i].string3[j] = '\0';
	}
	fscanf( INSTREAM, "%ld %s %s %s %s %ld %d", &data[i].number1, &data[i].string1,
	  &data[i].string2, &data[i].string3, &data[i].string4, &data[i].number2, &data[i].number3 );

        fgets( data[i].line1, 26, INSTREAM );
        if( data[i].number3 > 1 )
            fgets( data[i].line2, 26, INSTREAM );
        if( data[i].number3 > 2 )
            fgets( data[i].line3, 26, INSTREAM );
        if( data[i].number3 > 3 )
            fgets( data[i].line4, 26, INSTREAM );

        /*for ( j = 0; j < LINELENGTH; j++ )
            fscanf( INSTREAM, "%c", data[i].line1 );
        for ( j = 0; j < LINELENGTH; j++ ) {
            if( data[i].number3 > 1 )
	        fscanf( INSTREAM, "%c", data[i].line2 );
        }
        for ( j = 0; j < LINELENGTH; j++ ) {
            if( data[i].number3 > 1 )
                fscanf( INSTREAM, "%c", data[i].line3 );
        }
        for ( j = 0; j < LINELENGTH; j++ ) {
            if( data[i].number3 > 1 )
	        fscanf( INSTREAM, "%c", data[i].line4 );
        }*/
	printf( "%ld %s %s %s %s %ld\n", data[i].number1, data[i].string1, 
	  data[i].string2, data[i].string3, data[i].string4, data[i].number2, data[i].number3 );
	printf( "%s\n%s\n%s\n%s\n\n", data[i].line1, data[i].line2, data[i].line3, data[i].line4 );
	
	i++;
  }
  return 0;
}
Thanks in advance.