c - 如何在读取 C 中的几个字符串后读取带有空格的行的其余部分

标签 c input fgets

我的输入如下所示:

save 001 Some very long string with spaces... after three dots there , is also a comma

在此之后我调用类型:

load 001

它应该给我输出:

Some very long string with spaces... after three dots there , is also a comma

我正在尝试,但似乎没有任何效果,例如:

while ((scanf("%s %s", mode, key)) != EOF) {
    if (strcmp(mode, "save") == 0) { 
        getchar();
        fgets(data, 100, stdin);
        root = save(root, key, data);
        root = balance_RBT(root, blc);
    }
    if (strcmp(mode, "load") == 0) {
        load(root, key); 
    }
}

最佳答案

给定的输入/输出没有任何问题,代码的其余部分类似于:

#include <stdio.h>
int main() 
{ 
  char mode[444], key[333], data[222];
  while (1)
  while ((scanf("%s %s", mode, key)) != EOF) {
    if (strcmp(mode, "save") == 0) { 
      getchar();
      fgets(data, 100, stdin);
      printf("SAVE %s %s %s\n",mode,key,data);
    //root = save(root, key, data);
    //root = balance_RBT(root, blc);
    }
   if (strcmp(mode, "load") == 0) {
     printf("LOAD %s %s\n",mode,key);
     // load(root, key); 
     }
   }
 }

因此,无论您遇到什么问题,它们都必须处理您对模式、键和数据的定义,或者树插入/搜索的实现。

关于c - 如何在读取 C 中的几个字符串后读取带有空格的行的其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13430495/

相关文章:

c - 添加mysql客户端库出现段错误

c - 将 CSV 文件读取到 C 中的二维双数组

c - 从文件中读取文本行

c - 为什么代码只输出 'Have a nice day?' 尽管输入了 Friday 并声明了给定的字符串?

c - 这是正确的还是有更好的方法?

c - 错误的指令 - C 代码中的内联汇编语言

jQuery 选择器仅选择第一个元素

html -::-webkit-inner-spin-button 在 Firefox 上显示

javascript - 如何使用 JavaScript/HTML 通过下拉菜单和文本框定向 URL

C:从用户读取成对的整数直到换行(scanf、sscanf、fgets 和 friend )