I have a problem when writing a file on the text. As you could see, I used \n to put another set of my data on the next line. The problem is when i close the file and save again the data per line which ends with \n becomes \n\n and so on. That's why my file looks like this
FIRST SAVE
test, test, test
test, test, test
SECOND SAVE
test, test, test
test, test, test
THIRD SAVE
test, test, test
test, test, test
that's why when i display it on the screen... there are garbage value in between... My code is as follows:
save(){
int i = 0;
FILE *stream = NULL;
stream = fopen("student.txt", "wt");
printf("\nSaving the student list directory. Wait a moment please...");
printf("\nExiting the program...");
for (i=0; i<recordCtr; i++){
fprintf(stream, "%s, %s, %s\n", array[i]->studentID, array[i]->name, array[i]->course);
}
}
Help please... any suggestions will be appreciated. Thank you in advance.
array[i]->course? Does it have a newline at its end? What is inarray[i]->studentID? Does it have a newline at its beginning?array[i]->coursehas another newline terminator. Hint:fgets()reads the whole line together with the newline terminator. How exactly are you "resaving" the file?