linux fgets的问题

为什么我从键盘输入admin进行比较,结果i却是-1?

第1个回答  2014-01-28

因为

a="admin\0";
b="admin\n\0";

建议用fscanf函数

Linux 下 输入 man fgets 查看 fgets 函数手册可以知道:

   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 ('\0') is stored after the last character in the buffer.

所以fgets()连'\n'一起存储。

本回答被网友采纳
第2个回答  2014-01-29
如果只是看是否相等,改为int i=strncmp(a,b,5);本回答被提问者采纳
第3个回答  2014-02-10
int main()
{
char a[80] = "admin";
char b[80] = " ";
fgets(b,strlen(a)+1,stdin);
int i = str.....;
}
相似回答
大家正在搜