c - 在 while 循环中使用 scanf 读取带有空格的用户输入?

标签 c string file io scanf

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

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,Folders,用于存储相关论文的文件夹,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 会取得更好的成功。

char command[101];   

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

关于c - 在 while 循环中使用 scanf 读取带有空格的用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57227905/

相关文章:

Linux Shell 脚本剪切包含路径的变量的最后一部分

java - CompareTo 预期元素错误

python - 如何在 Python 中使用 JSON 从文件的特定列表中检索特定字符串

java - 在 Java 文件中写入多行有困难

c++ - 类和文件阅读

c - 当我从 C 文件中读取数字时,为什么会收到忽略返回值 ‘fscanf’ 的警告?

c - 如何从C中的txt文件中读取每n行

创建一个不是创建它的进程的子进程

java - 哈夫曼编码完成后如何用Java写入文件

c - 具有动态内存分配的 C 追加函数