|
|
1
27
您要执行以下操作:
也就是说,当您输入字符串并按“enter”键时,换行符将成为
或者,使用strncmp(),它只比较字符串的n个字符 |
|
|
2
9
fgets()返回字符串“exit\n”——与get()不同,它保留换行符。 |
|
|
3
6
正如其他人所说,与
比较的简单答案是准确的
在一般情况下,最好将I/O、标记化、解析和操作分离到各自的阶段。 |
|
|
4
1
同意戴夫的观点。此外,您可能希望改用strncmp()。然后可以设置比较的长度。 http://www.cplusplus.com/reference/clibrary/cstdio/fgets/ http://www.cplusplus.com/reference/clibrary/cstring/strncmp/ |
|
|
5
0
我建议您将\n从字符串的末尾剥离,如下所示。 char buf[256];
int len;
/* get the string, being sure to leave room for a null byte */
if ( fgets(buf,sizeof(buf) - 1) == EOF )
{
printf("error\n");
exit(1);
}
/* absolutely always null-terminate, the easy way */
buf[sizeof(buf) - 1] = '\0';
/* compute the length, and truncate the \n if any */
len = strlen(buf);
while ( len > 0 && buf[len - 1] == '\n' )
{
buf[len - 1] = '\0';
--len;
}
这样,如果必须将输入的字符串与几个常量进行比较,则不必将\n添加到所有常量中。 |
|
|
MaPo · Linux,设置锁定ICMP_过滤器选项 9 月前 |
|
Doohyeon Won · 内联函数上的奇怪现象?[关闭] 9 月前 |
|
|
Bobby · 复合字面值总是左值吗? 10 月前 |
|
9-Pin · C: 嵌套结构的堆栈内存分配 10 月前 |