Please help me... after the execution of the program. It write the new information to it.
However, the .txt file do not display the new information properly. (The index number stick with the name...). It is a half work program. Please help me solve this problem or else i cannot continue to finish whole program. :(
#include<stdio.h>
#define size 99
void maintenance();
struct newI{
char iN[9];
char name[50];
char gender;
};
void main()
{
FILE *buffer1;
buffer1 = fopen("students.txt", "w+");
fprintf(buffer1,"Index Number Name Gender");
fprintf(buffer1,"\n11228ADIA Alice Lim F ");
fprintf(buffer1,"\n12233ADIB Mike Chan M ");
fprintf(buffer1,"\n13244BASD Bobby M ");
fprintf(buffer1,"\n43134QWER Cheh Yen Lun M ");
fprintf(buffer1,"\n56354VBFG Choong Wen Loong M ");
fprintf(buffer1,"\n47344FGKJ Chin Pau Soon M ");
fclose(buffer1);
printf("File students.txt created!!!\n");
maintenance();
}
void maintenance()
{
struct newI newS[size];
int i=0;
char response;
char response2;
printf("To add new information key \'Y\' else press any key to skip>>>");
response=getchar();
if(response == 'Y' || response == 'y');
{
FILE *buffer2;
buffer2 = fopen("students.txt", "a+");
do
{
i++;
printf("\nFor student #%d",i);
printf("\nEnter student's index number>>>");
fflush(stdin);
gets(newS[i].iN);
printf("\nEnter student's name>>>");
fflush(stdin);
gets(newS[i].name);
printf("\nEnter student's gender(either *M* for male or *F* for female only)!!!>>>");
fflush(stdin);
scanf("%c",&newS[i].gender);
while(newS[i].gender!='M' && newS[i].gender!='F')
{
printf("\nInvalid input for gender.\nPlease reenter a proper gender as shown>>>");
fflush(stdin);
scanf("%c",&newS[i].gender);
}
printf("\nDo you still wish to add more student's information");
printf("\nTo add new information key \'Y\' else press any key to skip>>>");
fflush(stdin);
response2=getchar();
fprintf(buffer2,"\n");
fprintf(buffer2,"%s", newS[i].iN);
fprintf(buffer2," %s", newS[i].name);
fprintf(buffer2," %c", newS[i].gender);
}while(response2=='y' || response2=='Y');
fclose(buffer2);
}
}