do
	{
		ReadFile = fopen("StudentDetails.txt", "r");
		if(!ReadFile)
		{
			system("cls");
			printf("\n");
			printf("\t\t   ***************************************\n");
			printf("\t\t   ***************************************\n");
			printf("\t\t   ***                                 ***\n");
			printf("\t\t   ***           WELCOME TO            ***\n");
			printf("\t\t   ***    CLASS ATTENDANCE SYSTEM      ***\n");
			printf("\t\t   ***                                 ***\n");
			printf("\t\t   ***************************************\n");
			printf("\t\t   ***************************************\n");
			printf("\n\n");

			printf("\t\t   This File Cannot Be Open Successfully!!\n");
			printf("\t\t   Please Check It!!\n");
			exit(-1);
		}

		++temp_week;
		system("cls");
		printf("\n");
		printf("\t\t   ***************************************\n");
		printf("\t\t   ***************************************\n");
		printf("\t\t   ***                                 ***\n");
		printf("\t\t   ***           WELCOME TO            ***\n");
		printf("\t\t   ***    CLASS ATTENDANCE SYSTEM      ***\n");
		printf("\t\t   ***                                 ***\n");
		printf("\t\t   ***************************************\n");
		printf("\t\t   ***************************************\n");
		printf("\n\n");

		printf("\t\t   ******** MARKING OF ATTENDANCE ********\n");
		printf("\t\t                     PRESENT = 1  ABSENT = 0\n\n");
		for(i=0; fscanf(ReadFile,"%d|", &Student[i].studNo) != EOF; i++)
		{
			fscanf(ReadFile, "%d", &Student[i].regNo.year);
			fscanf(ReadFile, "%c", &Student[i].regNo.campus);
			fscanf(ReadFile, "%c", &Student[i].regNo.school);
			fscanf(ReadFile, "%c", &Student[i].regNo.level);
			fscanf(ReadFile, "%d|", &Student[i].regNo.serial_number);
			fscanf(ReadFile, "%[^|]|", &Student[i].student_Surname);
			fscanf(ReadFile, "%[^|]|", &Student[i].student_givenName);
			fscanf(ReadFile, "%[^|]|", &Student[i].gender);
			fscanf(ReadFile, "%[^|]|", &Student[i].father_name);
			fscanf(ReadFile, "%[^|]|", &Student[i].address1);
			fscanf(ReadFile, "%[^|]|", &Student[i].address2);
			fscanf(ReadFile, "%s", &Student[i].address3);

			printf("\t\t %d ", Student[i].studNo);
			printf("%d", Student[i].regNo.year);
			printf("%c", Student[i].regNo.campus);
			printf("%c", Student[i].regNo.school);
			printf("%c", Student[i].regNo.level);
			printf("%d ", Student[i].regNo.serial_number);
			printf("%c ", Student[i].gender);
			printf("%-10s ", Student[i].student_Surname);
			printf("%-10s ", Student[i].student_givenName);
			printf(": ");
			scanf("%d", &temp_Att);
			while(temp_Att != 1 && temp_Att != 0)
			{
				printf("\n\t\t   You Have Enter Out of Range\n");
				printf("\t\t   Please Re-enter Again\n");
				printf("\t\t\t\t\t\t\t: ");
				scanf("%d", &temp_Att);
			}
[B]			fprintf(AppendingFile, "%d ", temp_Att);
		}
		fprintf(AppendingFile, "\n");[/B]		
                  printf("\n");
		printf("\n\t Do You Want To Continue Mark Attendance(0 to Stop; 1 to Access)? ");
		scanf("%d", &temp_markatt);
		printf("\n\n");
		fclose(ReadFile);
	}while(temp_markatt != 0);

the problem is after i assign the value into my text file,
i'll get incorrect result, which i have a space before the '\n' when end of the line.
but i want is when end of the line is '\n'
example,

1 0 1 0 1<space>'\n'  ---- Incorrect
1 0 1 0 1'\n' ----- Correct

Edited 6 Years Ago by gahhon: n/a

One way to solve it is just before fprintf(AppendingFile, "\n"); call fseek() to move the file pointer backwards one character., such as fseek(AppendingFile,-1,SEEK_CUR);

fseek(AppendingFile,-1,SEEK_CUR);
this statement is it just like the '\b' ?
but what is SEEK_CUR ?

dude, that statement doesn't work.
it it still remain the <space> in my text file.

FILE *ExistingFile, *AppendingFile, *ReadFile, *ReadAttFile;

	ExistingFile = fopen("Attendance.txt", "w");
	AppendingFile = fopen("Attendance.txt", "a");
	ReadAttFile = fopen("Attendance.txt", "r");

Forget the fseek(). Just don't output the space when you've output the last value.

Don't output the value and the space together. Output them separately so you can control each of them.

When a file is opened in append mode, like AppendFile is, every write operation is done at the end of the file. Therefore fseek() will not do anything to resolve your problem. Open that file in "r+" mode then seek to end of file before writing anything.

>>Forget the fseek(). Just don't output the space when you've output the last value.
I would normally agree with that, but in this case it may not be possible to detect when the last item may be written. It would just be a lot simpler to back the pointer up one character then overwrite it with '\n'.

which mean there is no others way to solve this kind of problem ??
because my looping is an even loop, is depends on user want to continue or not..
if doing like that way.. many things have to be change..

now i fix the text file problem..
but after that, when i read from the text file and display out..
the alignment got problem..
the first row display should have 5 column, but it give me 3 column...
if i appending 3 attendance into the text file, in other view,
if i appending 5 attendace or more into the text file, the alignment will be correct.

void Mark_Attendance()
{
int tempstudent, answer, temp_Att, temp_markatt, temp_week = 6;
char ch;

FILE *ExistingFile, *AppendingFile, *ReadFile, *ReadAttFile;

ExistingFile = fopen("Attendance.txt", "w");
AppendingFile = fopen("Attendance.txt", "a");
ReadAttFile = fopen("Attendance.txt", "r");

if(!ExistingFile || !AppendingFile || !ReadAttFile)
{
	system("cls");
	printf("\n");
	printf("\t\t   ***************************************\n");
	printf("\t\t   ***************************************\n");
	printf("\t\t   ***                                 ***\n");
	printf("\t\t   ***           WELCOME TO            ***\n");
	printf("\t\t   ***    CLASS ATTENDANCE SYSTEM      ***\n");
	printf("\t\t   ***                                 ***\n");
	printf("\t\t   ***************************************\n");
	printf("\t\t   ***************************************\n");
	printf("\n\n");

	printf("\t\t   This File Cannot Be Open Successfully!!\n");
	printf("\t\t   Please Check It!!\n");
	exit(-1);
}
else
{
	fprintf(ExistingFile, "1 0 0 1 1\n");
	fprintf(ExistingFile, "1 1 1 1 1\n");
	fprintf(ExistingFile, "0 0 1 1 0\n");
	fprintf(ExistingFile, "1 0 0 1 1\n");
	fprintf(ExistingFile, "1 1 0 0 1\n");
	fprintf(ExistingFile, "1 0 1 0 1\n");

	fclose(ExistingFile);
	List_Student_Details();
	tempstudent = i;
	student = i + 1;		 
	do
	{
		ReadFile = fopen("StudentDetails.txt", "r");
		if(!ReadFile)
		{
			system("cls");
			printf("\n");
			printf("\t\t   ***************************************\n");
			printf("\t\t   ***************************************\n");
			printf("\t\t   ***                                 ***\n");
			printf("\t\t   ***           WELCOME TO            ***\n");
			printf("\t\t   ***    CLASS ATTENDANCE SYSTEM      ***\n");
			printf("\t\t   ***                                 ***\n");
			printf("\t\t   ***************************************\n");
			printf("\t\t   ***************************************\n");
			printf("\n\n");

			printf("\t\t   This File Cannot Be Open Successfully!!\n");
			printf("\t\t   Please Check It!!\n");
			exit(-1);
		}

		++temp_week;
		system("cls");
		printf("\n");
		printf("\t\t   ***************************************\n");
		printf("\t\t   ***************************************\n");
		printf("\t\t   ***                                 ***\n");
		printf("\t\t   ***           WELCOME TO            ***\n");
		printf("\t\t   ***    CLASS ATTENDANCE SYSTEM      ***\n");
		printf("\t\t   ***                                 ***\n");
		printf("\t\t   ***************************************\n");
		printf("\t\t   ***************************************\n");
		printf("\n\n");

		printf("\t\t   ******** MARKING OF ATTENDANCE ********\n");
		printf("\t\t                     PRESENT = 1  ABSENT = 0\n\n");
		for(i=0; fscanf(ReadFile,"%d|", &Student[i].studNo) != EOF; ++)
		{
			fscanf(ReadFile, "%d", &Student[i].regNo.year);
			fscanf(ReadFile, "%c", &Student[i].regNo.campus);
			fscanf(ReadFile, "%c", &Student[i].regNo.school);
			fscanf(ReadFile, "%c", &Student[i].regNo.level);
			fscanf(ReadFile, "%d|", &Student[i].regNo.serial_number);
			fscanf(ReadFile, "%[^|]|", &Student[i].student_Surname);
			fscanf(ReadFile, "%[^|]|", &Student[i].student_givenName);
			fscanf(ReadFile, "%[^|]|", &Student[i].gender);
			fscanf(ReadFile, "%[^|]|", &Student[i].father_name);
			fscanf(ReadFile, "%[^|]|", &Student[i].address1);
			fscanf(ReadFile, "%[^|]|", &Student[i].address2);
			fscanf(ReadFile, "%s", &Student[i].address3);

			printf("\t\t %d ", Student[i].studNo);
			printf("%d", Student[i].regNo.year);
			printf("%c", Student[i].regNo.campus);
			printf("%c", Student[i].regNo.school);
			printf("%c", Student[i].regNo.level);
			printf("%d ", Student[i].regNo.serial_number);
			printf("%c ", Student[i].gender);
			printf("%-10s ", Student[i].student_Surname);
			printf("%-10s ", Student[i].student_givenName);
			printf(": ");
			scanf("%d", &temp_Att);
			while(temp_Att != 1 && temp_Att != 0)
			{
			         printf("\n\t\t   You Have Enter Out of Range\n");
				printf("\t\t   Please Re-enter Again\n");
				printf("\t\t\t\t\t\t\t: ");
				scanf("%d", &temp_Att);
			}
			fprintf(AppendingFile, "%d", temp_Att);
			if(i == tempstudent - 1)
				fprintf(AppendingFile, "\n");
			else
				fprintf(AppendingFile, " ");		}
		printf("\n");
		printf("\n\t Do You Want To Continue Mark Attendance(0 to Stop; 1 to Access)? ");
		scanf("%d", &temp_markatt);
		printf("\n\n");
		fclose(ReadFile);
	}while(temp_markatt != 0);

	fclose(AppendingFile);
	printf("\t ******** ATTENDANCE COMPLETED AND SAVED ... THANK YOU!! ********\n\n");
	system("pause");
	system("cls");

	week = temp_week;

	x = 0;
	i = 0;

	while(fscanf(ReadAttFile,"%d%c", &Attendance[x][i], &ch) != EOF)
	{
		++i;
		if(ch == '\n')
		{
			++x;
			i=0;
		}
	}	for(i=0; i<student; i++)
	{
		studPresence[i] = 0;	
		for(x=0; x<week; x++)
			studPresence[i] += Attendance[x][i];
	}

	for(x=0; x<week; x++)
	{
		weekPresence[x] = 0;
		for(i=0; i<student; i++)
			weekPresence[x] += Attendance[x][i];
	}

PS: red colour is the problem;
green colour is the one i fixed append into text file.

This article has been dead for over six months. Start a new discussion instead.