c - How will 'fopen' interpret a 'char[]' whose capacity is larger than the string it contains? -
i writing program can encrypt text file.
first need ask users file name. did like:
char file_name[50]; fgets(file_name, 50, stdin);
but didn't work. how can this?
i confused if store file name in char array has, say, 50 elements, file name has 10 characters. when pass array or pointer fopen
, program going remaining 40 elements of array? storing value? passed fopen
?
in c, string \0
terminated sequence of characters. long there \0
within 50 bytes of file_name
, file_name
contains valid string fopen()
.
the reason fopen()
failed name passed had \n
in after reading input. because fgets()
stores newline character buffer. have remove before using string file name.
char *p = strrchr(file_name, '\n'); if (p) *p = '\0';
Comments
Post a Comment