Stuck on this for ages....Please Help!
I've remade the file so that now I can compile it. However, this time, it doesn't skip out the line
Code:
if (p_error==NULL)
{
printf("\nError in reading file"); /*EOF error*/
}
even when the following line condition is true:
Code:
while((p_error=fgets(str_copy,sizeof(str_copy),readfile))!=NULL)
Below is the code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSTUDENT 40
#define MAXNAME 20
typedef struct{
char name[MAXNAME+1];
int ex1,ex2,ex3,ex4,average;
}student_t;
int main(void)
{
FILE *readfile;
char str_copy[40];
char str_name[MAXNAME+1];
char *p_str;
int i,index;
student_t s [MAXSTUDENT];
char *p_error;
if((readfile=fopen("results.txt","rt"))==NULL)
{
printf("Error, unable to read file. Program exiting");
return (1);
}else
{
while((p_error=fgets(str_copy,sizeof(str_copy),readfile))!=NULL)
{
index=0;
if((p_str=strchr(str_copy,'\n'))!=NULL)
{
*p_str='\0';
printf("\nInput line OK");
} else
{
printf("\nInput line too long for buffer\n");
exit(0);
}
index++;
}
if (p_error==NULL)
{
printf("\nError in reading file"); /*EOF error*/
}
}
fclose(readfile);
return (0);
}
Please Can somebody help me? I dunno why this doens't work.:confused: