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);

}

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.

...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.