#include <stdio.h>
#define input "examdat.txt"
#define output "report.txt"
int fgetAnswers(int*pos, char res[], FILE*inp);
int main()
{
FILE*inp,*outp;
inp = fopen(input,"r");
outp = fopen(output,"w");
int n;
char ans[100];
int ID;
char result[100];
int missed[100];
int status = fgetAnswers(&n, ans, inp);
for(int m=1;m<n;m++)
missed[m]=0;
printf("%s/n",ans);
printf("ID score(%%)/n");
while(EOF != status)
{
status = fgetAnswers(&ID, result, inp);
double right = 0.0;
for(int i=1;i<=n;i++)
{
if(result[i] == ans[i])
{
right++;
}
else
{
missed[i]++;
}
}
fprintf(outp,"%d %d/n",ID,(int)(right/n*100));
printf("%d %d/n",ID,(int)(right/n*100));
}//while
printf("/n number:");
for(int j=1;j<n;j++)
printf("/t%d ",j);
printf("/n missed by:");
for(j=1;j<n;j++)
printf("/t%d ",missed[j]);
fclose(inp);
fclose(outp);
return 0;
}
int fgetAnswers(int*pos, char res[],FILE*inp)
{
fscanf(inp, "%d", pos);
char ch;
int i=0;
fscanf(inp, "%c",&ch);
while('/n' != ch)
{
res[i]=ch;
i++;
fscanf(inp, "%c",&ch);
}
int status = fscanf(inp, "%c",&ch);
res[i]='/0';
return status;
}
这段程序我在调试的时候又花了时间,主要是fgetAnswers()在返回读取状态时对于status的改变位置不对!
切记 其scanf()的用法,主要是返回状态的妙用!!!