C Programming help: What's wrong with my code? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

C Programming help: What's wrong with my code?

mlsbbemlsbbe Posts: 6Member
Can somebody tell me whats wrong with my code? My code doesn't skip out the line

[code]
if (p_error==NULL)
{
printf("
Error in reading file");
}
[/code]

even when the following line condition is true

[code]
while((p_error=fgets(str_copy,sizeof(str_copy),readfile))!=NULL)
[/code]

Below is my complete code:


[code]

#include
#include
#include

#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,'
'))!=NULL)

{
*p_str='';
printf("
Input line OK");
} else

{
printf("
Input line too long for buffer
");
exit(0);
}

index++;

}

if (p_error==NULL)/*This part of the code is not skipped even when p_error!=NULL*/
{
printf("
Error in reading file"); /*EOF error*/
}

}

fclose(readfile);
return (0);
}
[/code]


Thankyou for you help
Sign In or Register to comment.