Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 81
#define SIZEANS 81
int main(void)
{
FILE *fp, *outFp;
char *status, *status2;
int times;
int looptimes;
struct
{
char numberofItems[3];
char answersToQuestions[50];
} answerKey[SIZEANS];
struct
{
char studentID[7];
char answers[50];
} answer[SIZE];
int y, szAns[] = {0,3,51};
int i, sz[] = {0,6,51};
char buff[1024];
char buff2[1024];
char insert[1024];
int x = 0;
int w = 1;
int s = 1;
int temporary;
const int percent = 100;
int score = 0;
int trialpo;
fp = fopen("Examdat.txt","r");
outFp = fopen("outputData.txt","w");
status = fgets(buff, sizeof(buff), fp);
status2 = fgets(buff2,sizeof(buff2),fp);
for(times = 0; times<1;times++)
{
strncpy(answerKey[times].numberofItems, strtok(buff," "), szAns[1]);
strncpy(answerKey[times].answersToQuestions,strtok(NULL," "), szAns[2]);
printf("%s %s",answerKey[times].numberofItems,answerKey[times].answersToQuestions);
fprintf(outFp,"\tExam Report\nQuestion");
temporary = atoi(answerKey[times].numberofItems);
for (s = 1; s <= temporary; s++)
{
fprintf(outFp,"\t%d",s);
}
fprintf(outFp,"\nAnswer");
for(w = 0; w <= strlen(answerKey[times].answersToQuestions) - 2; w++)
{
printf("%c\n",answerKey[times].answersToQuestions[w]);
fprintf(outFp,"\t %c", answerKey[times].answersToQuestions[w]);
}
}
fprintf(outFp,"\n\nID\t\tScore(%)\n");
while(status2 != NULL)
{
strncpy(answer[i].studentID, strtok(buff2," "),sz[1]);
strncpy(answer[i].answers, strtok(NULL," "),sz[2]);
fprintf(outFp,"%s", answer[i].studentID);
printf("%d",strlen(answer[i].answers) - 1 );
for(x = 0; x<=strlen(answer[i].answers) - 1; x++)
{
if(answer[i].answers[x] == answerKey[0].answersToQuestions[x])
{
score += 1;
printf("%c",answer[i].answers[x]);
printf("%c\n",answerKey[0].answersToQuestions[x]);
}
}
x = 0;
status2 = fgets(buff2,sizeof(buff2),fp);
//printf("%d\n",score - 2);
//score /= temporary;
//score *= percent;
fprintf(outFp,"\t%d\n",score);
i++;
}
printf("\n");
fclose(outFp);
fclose(fp);
return 0;
}
The problem is now that every time I put the string length of each of the text file, it's giving me a 665