c - fprintf outputting ')' in txt file -
i have been trying create c program print current content of .txt file, allow user enter content wish there instead, , print content on previous txt file. resulting .txt file has ')' printed, replacing characters is printed.
#include <stdio.h> #include <stdlib.h> int main(void) { file *filedisplay = fopen("password1.txt", "r" ); char c; printf("current password is: "); do{ c = fgetc(filedisplay); printf("%c", c); } while (c != eof); fclose(filedisplay); char np[]=""; printf("\nplease enter new password: \n"); scanf(" %s", np); file *file = fopen("password1.txt", "w" ); fprintf(file," %s", np); fclose(file); return 0; } for example, if user inputs
password
as char np, output fprintf is
p')'uord
the array np has room 1 character (the terminating '\0' of empty string "" initialize with), since not specify size it. can not fit string other empty string , rest of user's input overflows, causing undefined behaviour.
you need provide array large enough hold user's input (specify size between []) , should inform input function (here scanf, fgets might better here) of size knows not write past end of array.
Comments
Post a Comment