printf - C Programming fprintf issue -
i have problem when writing file on text. see, used \n put set of data on next line. problem when close file , save again data per line ends \n becomes \n\n , on. that's why file looks 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 display on screen... there garbage value in between... code follows:
save(){ int = 0; file *stream = null; stream = fopen("student.txt", "wt"); printf("\nsaving student list directory. wait moment please..."); printf("\nexiting program..."); (i=0; i<recordctr; i++){ fprintf(stream, "%s, %s, %s\n", array[i]->studentid, array[i]->name, array[i]->course); } } help please... suggestions appreciated. thank in advance.
not sure of if parse file before saving again forgetting remove old \n original last string..
edit: right. op uses fgetsfunction, includes line terminator.
so starting "test, test, test\n" strtok obtain "test" "test" "test\n" when saved new newline (forget it) added file.
you fix setting last character null with
linebuffer[strlen(linebuffer)-2] = '\0' (it safe since fgets return null-terminated string itself)
you can add \n delimiters used, should end same behavior (not sure empty tokens strtok iirc discarded).
Comments
Post a Comment