【问题标题】:Reading in user input with space using scanf in a while loop?在while循环中使用scanf读取带有空格的用户输入?
【发布时间】:2019-12-05 06:18:43
【问题描述】:

我正在编写一些代码,我试图在以下命令中读取这些代码,这将导致我的程序中的某些函数被调用:

PRINT
INSERT 0,Folders,Folders for storing related papers,25
PRINT
QUIT

我一直在尝试不同的方式来读取这个输入,它来自./inventory test02/inventory02-actual.txt < test02/input02.txt > test02/actual02.txt,上面显示的这些命令在文件 input-02.txt 中。

我主要与scanf 合作,但也尝试过fgets,但我在scanf 方面取得了最大的成功。我最初尝试scanf("%s", command),在这种情况下,scanf 不接受空格,因此程序终止。

//Keep re-prompting user for commands until you reach EOF{
  while ((scanf("%[0-9a-zA-Z, ]s", command) == 1)) {  
    printf("====================\nCommand? ");
    printf("%s\n", command);      
    if (strcmp(command, "PRINT") == 0) {     
      print(list);    
    } else if (strcmp(substring(command, START, INSERT_END), "INSERT") == 0) {    
      //Get the substring - this is the input after "INSERT"
      char string[MAX_LEN_COMMAND];
      strcpy(string, substring(command, OFFSET, strlen(command)));                     
      insert(string, list);   
    } else if (strcmp(command, "PRINTREVERSE") == 0) {
      printReverse(list);
    } else {
      printf("Invalid command passed.\n"); 
      exit(EXIT_BAD_INPUT);
    }
  }

目前,当我运行我的代码时,只读取了第一个命令“PRINT”。似乎我无法从 input-02.txt 读取下一行输入。有没有办法可以正确读取这些命令?此外,在我的程序读入“INSERT”之后,它会读入“0,文件夹,用于存储相关文件的文件夹,25”作为命令,这是不应该的。它应该直接转到下一个命令,即“PRINT”。在调用 insert 方法后,我尝试使用 continue 语句,但这不起作用。有人有什么建议吗?

编辑:使用 fgets 更新代码。

我认为传递printf 向我们展示命令的含义,而不是发布我在上面调用的所有函数,这对于一个可重复的示例来说可能足够简单!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define OFFSET 7
#define START 0
#define INSERT_END 5

static const char *substring(char command[], int start, int end);

int main(int argc, char **argv) 
{
  //Get user input for commands    
  char command[500];

  //Keep re-prompting user for commands until you reach EOF{
  while (fgets(command, sizeof(command), stdin) != NULL) {  
    printf("====================\nCommand? ");
    printf("%s\n", command);      
    if (strcmp(command, "PRINT") == 0) {     
      printf("%s", command);    
    } else if (strcmp(substring(command, START, INSERT_END), "INSERT") == 0) {    
      printf("%s", command);   
    } else if (strcmp(command, "PRINTREVERSE") == 0) {
      printf("%s", command);
    } else {
      printf("Invalid command passed.\n"); 
      exit(1);
    }
  }
}

static const char *substring(char command[], int start, int end) 
{
  int i = 0;
  int j = 0;
  char *sub;
  sub = (char *)malloc(500 * sizeof(char));
  for (i = start, j = 0; i <= end; i++, j++) {
    sub[j] += command[i];
  }
  sub[j] = '\0';
  return sub;
}

我得到的输出是:

====================
Command? PRINT

Invalid command passed.

【问题讨论】:

  • 建议:我知道你说你有问题,但是:一定要选择fgets。用scanf 做你想做的事情几乎是不可能的。
  • 嗨@SteveSummit,有没有实现fgets的好方法?比如我用fgets(command, sizeof(command), stdin),连第一个命令都看不懂。
  • 关于fgets,如果command 是char* 而不是char[N],那么是的,这行不通,因为我们看不到你做了什么和上下文你在...是的。使用失败的fgets 中的minimal reproducible example 更新您的问题。它应该可以通过包含堆栈、单个函数、main 以及每个中的六行代码来重现。除此之外的任何事情都是矫枉过正和/或不必要的。
  • 嗨@WhozCraig,我已经用一些可以在本地运行的代码对上面的问题进行了更新。
  • @PomegranateSociety 谢谢。现在您所需要的只是了解fgets 的工作原理。它读取整个行。一旦你有了它,然后你可以sscanf输入特定内容。 (或者如果这是您的出价,则解析纯手动)。重要的是:fgets 保留在控制台上输入的换行符或从文件中读取,因此除非您希望将其作为比较数据的一部分,否则您需要清除它(通常使用 strpbrk 和硬设置 nullchar 或一些类似的操作)。

标签: c string file io scanf


【解决方案1】:

由于您正在阅读一行,使用fgets 将获得更好的成功。

char command[101];   

while (fgets(command, 100, stdin))
{
// rest of the code can be the same
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2015-01-19
    • 1970-01-01
    • 2011-09-08
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多