简体   繁体   English

如何在C中创建命令行?

[英]How to create a command line in C?

I'm trying to create a command line for an app in C. 我正在尝试为C中的应用创建命令行。

int main(int argc, char const *argv[])
{
  char cmd[40];

  do {
    scanf("%[^\n]", cmd);

    if(strcmp(cmd, "start"))
      new_game();
  } while(strcmp(cmd, "exit"));
  return 0;
}

I can't use the "%s" because the command might have more than one argument so I need to use the exception and ignore the new line char. 我不能使用“%s”,因为该命令可能有多个参数,因此我需要使用异常并忽略换行符。 The problem is that the program won't call the scanf anymore after one insertion. 问题在于,该程序在插入一次后将不再调用scanf。 Why is this happening? 为什么会这样呢?

Thank You 谢谢

Use fgets instead to read the whole input as shown below: 改用fgets读取整个输入,如下所示:

char cmd[255];
fgets(cmd, 255, stdin);

The sample can be found here . 样本可以在这里找到。


Also, the strcmp function is not used correctly in your example. 另外,您的示例中未正确使用strcmp函数。 Strcmp returns: Strcmp返回:

  • <0 when the first character that does not match has a lower value in ptr1 than in ptr2, 当第一个不匹配的字符在ptr1中的值小于ptr2中的值时,<0,
  • 0 when they are equal 当它们相等时为0
  • '>0 when the first character that does not match has a greater value in ptr1 than in ptr2 '> 0,如果第一个不匹配的字符在ptr1中的值大于在ptr2中的值

So in your example, you should use strcmp(str1, str2) == 0 or !strcmp(str1, str2) for short. 因此,在您的示例中,应该使用strcmp(str1, str2) == 0或简称为!strcmp(str1, str2)


Note: Care should be taken when using fgets . 注意: 使用fgets时应小心。 A newline character is considered valid by fgets and it is included in the string. fgets认为换行符有效,并且包含在字符串中。 On the other side with scanf(" %s") a newline is not included in the resulting string. 另一方面,在scanf(" %s")的结果字符串中不包含换行符。

strcmp(cmd, "start") means "when cmd does not equal start". strcmp(cmd, "start")意思是“当cmd不等于start时”。 You need !strcmp(cmd, "start") . 您需要!strcmp(cmd, "start")

The program jams because you read everything up to the first newline, but then you go back again and the scanf() finds a newline, which isn't allowed by the scan set, so it reads no characters and leaves the newline in place for the next time round the loop. 该程序阻塞,因为您读取了所有内容,直到第一个换行符为止,但是您再次返回, scanf()找到了一个换行符,这是扫描集所不允许的,因此它不读取任何字符,并将换行符留在原处下一次循环。

You should be checking the return value from scanf() ; 您应该检查scanf()的返回值; if it is zero, then the next character isn't valid for the format string. 如果为零,则下一个字符对于格式字符串无效。

You have to read the newline. 您必须阅读换行符。

One simple way is to include a space before the %[…] scan set in the format string. 一种简单的方法是在格式字符串中的%[…]扫描集之前包含一个空格。 That skips leading white space, including newlines, before reading the rest of a line of information (and the 39 prevents buffer overflows): 在读取其余信息行之前,这会跳过前导空白,包括换行符(而39防止缓冲区溢出):

if (scanf(" %39[^\n]", cmd) == 1)
    …process the string in cmd…
else
    …report error…

Another simple way is to use a loop after the scanf() : 另一种简单的方法是在scanf()之后使用循环:

int c;
while ((c = getchar()) != EOF && c != '\n')
    ;

Alternatively, as others have suggested, read a line of input and then process that, perhaps with sscanf() to read the information into your command name variable. 或者,如其他人所建议的那样,读取一行输入,然后使用sscanf()处理该行,以将信息读取到您的命令名变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM