Here's how it works: It enters the first while loop, reads the first line, stores it in the "line" array, then makes x=1, then it does "nothing" because of the
if( x == 1 )
{ //nothing in here
}
so it enters the first while loop again, reads the next line, stores it in the "line" array, makes x = 2, then (x==1) is false so it executes your code in the "else" block.
OK, it works, but it's very clumsy. First, the line[100][100] is an error. There's no reason at all for a 100 x 100 array, and it's actually the wrong type to give as an argument to fgets. Your compiler should be giving you a warning about that. line[100] would be better, but still not so good.
The real issue is that there is no reason to be storing that "junk" at all (except that fgets requires a buffer to store what it reads). Here's a much nicer way to read and ignore those 2 lines:
for( i = 0; i < 2; i++ ) {
while( fgetc(infile) != '\n' )
;
}
After that goes your code to read and print the read data, with no extra variable "x", no "line" array, no if statements, etc.
One more thing: you should use "int", not "void" as the return type of your main function. I guess you are using Turbo C++ or some other very old compiler which doesn't insist on that, but int is thestandard so you should get in the habit of using it. In other words, write int main() or int main(void) , and then put return 0; as the last line of your program.
By the way, you omitted brackets for your second while loop. Also, I have to agree with ssharish2005 that your indentation needs work. But I'll give you a break this time. Here's how your program should look:
#include <stdio.h>
int main (void)
{
char matrix[20][200];
int i, j, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20], final[20];
FILE *infile;
infile = fopen("studentsmark.txt","r");
for( i = 0; i < 2; i++ ) {
while( fgetc(infile) != '\n' )
;
}
i=0;
while( fscanf(infile,"%d %s %d %d %d %d %d %d",&no[i],matrix[i], &quiz_1[i], &quiz_2[i], &quiz_3[i], &project[i], &midT[i], &final[i]) !=EOF)
{ printf("%d %s %d %d %d %d %d %d\n",no[i], matrix[i], quiz_1[i], quiz_2[i], quiz_3[i], project[i], midT[i], final[i]);
i++;
}
fclose(infile);
return 0;
}