From the fgets
man page:
fgets()
reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. [...]
You need to strip the end-of-line marker from the string, otherwise you'll end up with a filename that contains it (and most shells/implementations of ls
will replace that with a ?
by default).