I am a beginner in C. I am making a simple game in c.
I have a .txt file that stores players' score such as
gse
12
CKY
8
Then I have this function in C and the function prints out score based on .txt file above.
int n = 0;
int c;
int p=0;
char name[NAME_LENGTH] = { 0 };
char score[NAME_LENGTH] = { 0 };
FILE *fp = fopen("scoreBoard.txt", "r");
if (fp == NULL) {
printf("no score available\n");
fflush(stdin);
getchar();
return;
}
system("cls");//clears the screen
if (fp){
while((c=getc(fp)!=EOF)){
if(n%2==0){
fgets(name,NAME_LENGTH,fp);
printf("%d ",n/2+1); //index
printf("%s",name);
n++;
}
if(n%2==1){
fgets(score,NAME_LENGTH,fp);
printf("%s",score);
n++;
}
}
fclose(fp);
}
printf("=======SCORE=======\n");
printf("Enter AnyKeys");
Sleep(100);
getchar();
getchar();
//fflush(stdin);
}
output is the following
1 se
12
2 KY
8
I tried many things but I can't figure it out.
I guess something is eating up the code. Is (c=getc(fp)!=EOF)
the problem? Should I manipulate pointer in order to fix this?
Thanks in advance.