它不是末尾的空白字符序列,而是换行符,因为fgets()它包含在返回的缓冲区中:将 the 替换\n为空终止符:
/* fgets() will not read the new-line if
there is not sufficient space in the buffer
so ensure it is present. */
char* nl_ptr = strrchr(buff, '\n');
if (nl_ptr) *nl_ptr = '\0';
由于明显的换行,它可能看起来好像有一系列空白字符stdout,但这是由于 . 读取的换行符的存在fgets()。
[]打印字符串时,我发现将字符串放入其中以使字符串的内容更清晰很有用:
printf("first line of \"conf\": [%s]\n", buff);
这将使通过获得的换行符的存在fgets()更加明显。
请注意,该函数webroot()正在返回局部变量的地址buff:这是一个错误并且是未定义的行为。一个新的缓冲区需要动态分配,strdup()如果可用malloc(),或者不使用strcpy():
return strdup(buff);
的调用者webroot()必须free()返回值。NULL如果发生故障,则安排返回。