I'm working on that education quizzer program that I got some hints on awhile ago. Now I'm adding the ability to read and write the program data on disk.
The problem that I am having is reading the data. When the data is displayed newlines are shown where there shouldn't be any. I think newlines are being added during the loading process.
Here's my code to load the data into the program
Code:
int loadDatabank(char filename[25])
{ //loads databank off disk
int ctr;
int i;
FILE *fPtr;
char buffer[80];
printf("Enter filename to be loaded: ");
scanf("%s", filename);
strcat(filename,".txt");
fPtr = fopen(filename, "r+");
if(!fPtr)
{ printf("A problem occurred whie trying to open the file.\n");
pressKey();
return 0;
}
fseek(fPtr,0L,SEEK_SET);
fgets(buffer,sizeof(buffer),fPtr);
sscanf(buffer,"%d",&numList);
for(ctr = 0;ctr <= numList; ctr++)
{
fgets(buffer,sizeof(buffer),fPtr);
sscanf(buffer,"%d",&list[ctr].type);
fgets(list[ctr].question,sizeof(list[ctr].question),fPtr);
if(list[ctr].type == QUEST_MULTIPLE)
{
fgets(buffer,sizeof(buffer),fPtr);
sscanf(buffer,"%d",&list[ctr].nChoices);
i = 0;
while(i < list[ctr].nChoices)
{
if(fgets(list[ctr].choices[i],sizeof(list[ctr].choices[i]),fPtr) == NULL)
break;
i++;
}
}
fgets(list[ctr].answer,sizeof(list[ctr].answer),fPtr);
strtok(list[ctr].answer,"\n");
}
fclose(fPtr);
printf("File successfully loaded.");
pressKey();
return 1;
}
The comments were added for this post.
File format:
Code:
4 /* total entries in file */
0 /* question type: 0 direct, 1 multiple choice, ect. */
What is 1 + 1? /* question*/
2 /*answer*/
2
Red is red.
true
3
-------- becomes before January.
February
1
The perimeter of a square is 24. What is the area?
3 /* number of choices */
36 sq. in.
6 sq. in.
60 sq. in.
A