0

Is there anything wrong with my coding? Why do I keep getting the message..."Error Opening Input File!"

#include <stdio.h>
#include <conio.h>
void  fgetAnswers(FILE*,int*,char[]);
void  fgetAnswers2(FILE*,int*,int,char[]);
int main()
{char ans[50],choices[50];
int n,i,id,missed[50],tot;
FILE *input,*output;
input = fopen("examdat.txt","r");
if(input == NULL)
  { printf("Error opening input file!\n");
    getch();
    return 0;
   }
fgetAnswers(input,&n,ans);
output = fopen("report.txt","w");
fprintf(output,"\tExam Report\n\nQuestion");
for(i=1;i<=n;i++)
    fprintf(output,"%3d",i);
for(i=0;i<n;i++)
    missed[i]=0;
fprintf(output,"\nAnswer ");
for(i=0;i<n;i++)    
   fprintf(output,"%3c",ans[i]);
fprintf(output,"\n\nID\tScore(%%)\n");
do
{fgetAnswers2(input,&id,n,choices);
tot=0;
if(id>=0)
   {for(i=0;i<n;i++)
      { if(choices[i]==ans[i])
          tot++;
       else
         missed[i]++;
       }
  fprintf(output,"%3d\t%3.0f\n",id,tot/(double)(n)*100.);
    }
}while(id>=0);
fprintf(output,"\nQuestion     ");
for(i=1;i<=n;i++)
     fprintf(output,"%3d",i);
fprintf(output,"\nMissed by   ");
for(i=0;i<n;i++)    
   fprintf(output,"%3d",missed[i]);    
fclose(input);
fclose(output);  

return 0;

}
void fgetAnswers2(FILE* input,int *n,int m,char ans[])
{
int i;
fscanf(input,"%d",n);
if ( feof(input) )
    {*n=-1;
    return;
    }
fgetc(input);
for(i=0;i<m;i++)
    ans[i]=fgetc(input);
}
void fgetAnswers(FILE* input,int *n,char ans[])
{
int i;
fscanf(input,"%d",n);
fgetc(input);
for(i=0;i<*n;i++)
    ans[i]=fgetc(input);

}

Edited by Nick Evan: Fixed formatting

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Daniel85
0

If you are running this from the debugger (or if the file is not in the same directory with the executable), you might need to give the full path of the file.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.