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

now i think that index number have accept the name as a part of it....but why??? since i put different variable.

This is the contents of the file that I got after correcting the program

Index Number          Name                         Gender
11228ADIA             Alice Lim                       F  
12233ADIB             Mike Chan                       M  
13244BASD             Bobby                           M  
43134QWER             Cheh Yen Lun                    M  
56354VBFG             Choong Wen Loong                M  
47344FGKJ             Chin Pau Soon                   M  

                              Melvin
                                                             M

                              Sandra
                                                             F
#include<stdio.h>
#include <ctype.h>
#define size 99

void maintenance();

struct newI{
	char iN[9];
	char name[50];
	char gender;
};

int 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();
             return 0;
}

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(stdout);
			fgets(newS[i].iN, sizeof(newS[i].iN), stdin);
			printf("\nEnter student's name>>>");
			fflush(stdout);
			fgets(newS[i].name, sizeof(newS[i].name), stdin);
			printf("\nEnter student's gender(either *M* for male or *F* for female only)!!!>>>");
			fflush(stdout);
			scanf("%c",&newS[i].gender);
			getchar(); // flush stdin of the '\n' character
			newS[i].gender = toupper(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(stdout);
				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(stdout);
			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);
	}
}
Comments
Must report you as a hard worker. ;) Hopefully poster will understand what are the changes.

Okay, thankyou Ancient Dragon...it finally work but another problem occurred...:(
Here is my new code:

#include<stdio.h>
#define size 99

void maintenance();

struct newI{
	char iN[10];
	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);
			fgets(newS[i].iN,(sizeof newS[i].iN),stdin);
			printf("\nEnter student's name>>>");
			fflush(stdin);
			fgets(newS[i].name,(sizeof newS[i].iN),stdin);
			printf("\nEnter student's gender(either *M* for male or *F* for female only)!!!>>>");
			fflush(stdin);
			newS[i].gender=getchar();
			while(newS[i].gender!='M' && newS[i].gender!='F')
			{
				printf("\nInvalid input for gender.\nPlease reenter a proper gender as shown>>>");
				fflush(stdin);
				newS[i].gender=getchar();
			}

			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();
			fflush(stdin);
			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);
	}
}

Another frustration~~:(

The output of .txt file is:

Index Number Name Gender
11228ADIA Alice Lim F
12233ADIB Mike Chan M
13244BASD Bobby M
43134QWER Cheh Yen Lun M
56354VBFG Choong Wen Loong M
47344FGKJ Chin Pau Soon M
1234ASDFA bobby
M
1234ASDFA sifa fu
F

Why the gender will display in another newline...is not what i want. :(

see lines 71 and 72 -- you have way too many spaces there. Remove all those spaces and I'll bet the gender will appear on the same line as the name. Its only a display thing -- the lines are too long so the os wraps them around.

If you want to create fixed length fields for the data then use the field-width specifiers in the print statement. printf("%15s", name); will print the name in a field of 15 characters left padded with spaces. use "%-15s" and it will be right padded with spaces.

Okay i try it out, but it messed up also... :(

Here what i change:

#include<stdio.h>
#define size 99

void maintenance();

struct newI{
	char iN[10];
	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  ");
	fprintf(buffer1,"\n");
	
	fclose(buffer1);
	printf("File students.txt created!!!\n");
	maintenance();
}

void maintenance()
{
	struct newI newS[size];
	int i=0;
	char response;
	char response2;
	
	fflush(stdout);
	fflush(stdin);
	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);
			fgets(newS[i].iN,(sizeof newS[i].iN),stdin);
			printf("\nEnter student's name>>>");
			fflush(stdin);
			fgets(newS[i].name,(sizeof newS[i].name),stdin);
			printf("\nEnter student's gender(either *M* for male or *F* for female only)!!!>>>");
			fflush(stdin);
			newS[i].gender = getchar();
			while(newS[i].gender!='M' && newS[i].gender!='F')
			{
				fflush(stdin);
				printf("\nInvalid input for gender.\nPlease reenter a proper gender as shown>>>");
				newS[i].gender = getchar();
			}

			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();
			fflush(stdin);
			fprintf(buffer2,"%s",newS[i].iN);
			fprintf(buffer2,"%17s",newS[i].name);
			fprintf(buffer2,"%25c",newS[i].gender);
		}while(response2=='y' || response2=='Y');
		fclose(buffer2);
	}
}

output more worst::::
Index Number Name Gender
11228ADIA Alice Lim F
12233ADIB Mike Chan M
13244BASD Bobby M
43134QWER Cheh Yen Lun M
56354VBFG Choong Wen Loong M
47344FGKJ Chin Pau Soon M
ASDFA1234 Siu Choong
MASDFA1234 Wendy
F

The reason for the wierd behavior is because fgets() appends the '\n' from keyboard input to the end of the string, so you need to remove it. Here is one way to do it

void fixup(char *s)
{
	if(s[strlen(s)-1] == '\n')
		s[strlen(s)-1] = 0;
}

...
			fgets(newS[i].iN,(sizeof newS[i].iN),stdin);
			fixup(newS[i].iN);

Just call fixup() after each fgets() to strip out the '\n' character.

void main()
That is a big no-no. main() always returns an int so it should be declared int main() . Just because your compiler may accept void doesn't mean it is correct.

line45: buffer2 = fopen("students.txt", "a+");

the semicolon after remove have error. :(

Sorry for double posting the same thing..i miss your post #13. About the fixup() function you written, i thought the fflush(stdin) will work the same thing, clear input.

fflush(stdin) is non standard -- the fflush() function is only intended for output streams, not input streams, although some compilers use non-standard extension. In any event, fflush() does not affect the input by fgets() function because fgets() removes everything up to and including the '\n' character assuming there is room for the characters in the input buffer.

erm, i don't know if i right, if the fgets() will removes everything up, then why would the program still append the '\n'?

okay, i try to understand your fixup funtion first. Hope it work... :)

OMG this is the problem i get while use the functions
warning C4013: 'strlen' undefined; assuming extern returning int

>warning C4013: 'strlen' undefined; assuming extern returning int
Make sure to add #include <string.h> to your code.

OMG this is the problem i get while use the functions
warning C4013: 'strlen' undefined; assuming extern returning int

strlen() is defined in the header file string.h. #include <string.h>

This question has already been answered. Start a new discussion instead.