我正在尝试使用 C 中的函数 fgets() 读取 8 个字符,并且必须是来自标准输入的 8 个字符。
到目前为止,我已经得到了这个
char HEX[9];
while (fgets(HEX, 8, stdin) != NULL)
{
if (HEX[8] == '\0')
{
printf("GOOD\n");
break;
}
else
{
printf("Not 8 characters");
}
}
但是它不起作用。有任何想法吗?还尝试使用 if (strlen(HEX) > 8) 但两者都没有。
提前致谢。
最佳答案
来自手册页:
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. A terminating null byte (aq\0aq) is stored after the last character in the buffer.
您只阅读了七个字符。
关于c - 在 C 中使用 fget 读取固定数量的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17956844/