I believe this will work better. Note that you were writing out the variables in a slightly different order than you were fscanf'ing them back in, and the failing message shouldn't go into the data file, because you have no way of handling it when the data is read back in.
Instead, just add an if statement to your program after the data is read back in from the file, and then print the "student has failed and can't promote" message, onto the screen.
#include<stdio.h>
#include<string.h>
#include<conio.h>
#pragma warning(disable: 4996)
float avg(float x);
int main()
{
FILE *fp;
int k,students=10,subjects=4,i,j,id_number,fail;
float marks[100],total=0,average;
char name[30],course_enrolled[30],filename[10],date_of_birth[30];
//printf("filename\n");
//scanf("%s",filename);
printf("\n\n\n\n");
fp=fopen("filename","w");
for(i=1;i<=10;i++)
{
printf("Enter Name of Student\n");
fscanf(stdin,"%s",name);
fprintf(fp,"%s\n",name);
printf("Enter Course In Which Student Is Enrolled\n");
fscanf(stdin,"%s",course_enrolled);
fprintf(fp,"%s\n",course_enrolled);
printf("Enter Date Of Birth\n");
fscanf(stdin,"%s",date_of_birth);
fprintf(fp," %s\n",date_of_birth);
printf("Enter ID Number\n");
fscanf(stdin,"%d",&id_number);
fprintf(fp," %d\n",id_number);
for(j=0;j<4;j++)
{
printf("Enter Marks for Subject\n");
scanf("%f",&marks[j]);
total=total+marks[j];
if(marks[j]<50)
fail=fail+1;
// else
// continue;
}
average=avg(total);
total=0.0;
printf("AVERAGE=%f\n",average);
fprintf(fp," %f\n",average);
if(average>=70)
printf("Grade A\n");
else if(average>=60&&average<70)
printf("Grade B\n");
else if(average>=50&&average<60)
printf("Grade C\n");
else if(average>=45&&average<50)
printf("Grade D\n");
else if(average>=40&&average<45)
printf("Grade E\n");
else if(average<40)
printf("Fail\n");
// else
// continue;
if(fail>=2)
{
printf("Student is failed and can't promote\n");
//fprintf(fp,"Student is failed and can't promote\n");
}
// else
// continue;
fail=0;
}
fclose(fp);
fp=fopen("filename","r");
for(k=1;k<=10;k++)
{
//printf("Input filename");
//scanf("%s",filename);
//fscanf(fp,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,&id_number,date_of_birth,average);
fscanf(fp,"%s %s %s %d %f",name,course_enrolled,date_of_birth, &id_number,&average);
fprintf(stdout,"NAME= %s\tCOURSE= %s\t ID= %d\t DOB= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average);
if(average < 40)
printf("\n*This student has failed and can't promote*\n");
}
fclose(fp);
getch();
return 0;
}
float avg(float x)
{
float a=0.0;
a=(x/400)*100;
return (a);
}