سؤال

When i execute fgets() function to following file:

code.txt

this is a test
foobar
all your base

it also copies '\n'.

هل كانت مفيدة؟

المحلول

This is by design:

The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a is read and transferred to s, or an end-of-file condition is encountered. (emphasis is mine)

If you would prefer to not get \n, use fscanf("%100s", str) (put the actual size instead of 100).

In C++, use std::string and std::getline for a much more flexible way of reading lines from a file.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top