【问题标题】:Reading String Until Blank Space读取字符串直到空格
【发布时间】:2021-02-23 01:37:03
【问题描述】:

我正在开发一个 C 程序,它将用户输入读取为用空格分隔的字符串。我想要一种方法来读取这些字符串,直到出现空格。我知道 scanf 很容易做到这一点,但我想知道是否可以使用 gets() 或 sscanf() 或 2 的某种组合来做同样的事情。我对 C 非常陌生,只是刚刚开始它,我来自 Java 背景。提前谢谢大家,我真的很感激任何和所有的帮助!!!!在下面找到我到目前为止的代码以及示例输入(***如果未注释 scanf() 部分,则可以实现所需的功能,但目标是仅使用 get() 实现与 scanf() 相同的功能( ) 和 sscanf())。

int main()
{
    printf("Enter words seperated by spaces:(. or EOF to stop):\n");

    do
    {
        char s[255];
        //scanf("%s",s);
        gets(s);
        if(strcmp(s,".")==0 || strcmp(s,"EOF")==0)
        {
            insert_dictionary_order(s);//adding to list
            break;
        }
        else
        {
            insert_dictionary_order(s);//adding to list
        }
    }
    while(1);
    //now printing list
    print_list();
    return 0;
} 
This is a sample text.
The file will be terminated by a single dot: .
The program continues processing the lines because the dot (.)
did not appear at the beginning.
. even though this line starts with a dot, it is not a single dot.
The program stops processing lines right here.
.
You won't be able to feed any more lines to the program.

编辑:答案必须实现 get() 或 sscanf() 或两者。

【问题讨论】:

    标签: arrays c string gcc io


    【解决方案1】:

    首先,永远,永远,永远使用gets()。它非常不安全并且容易被缓冲区溢出利用,它已从 C11 开始的 c 库中完全删除。请参阅Why gets() is so dangerous it should never be used!(使用scanf()"%s""%[..]" 转换说明符而没有可选的field-width 修饰符只是一个坏的)

    fgets() 是您正在寻找的。要读取您的文本文件,当'.' 单独作为一行中的第一个字符出现时停止(除了fgets() 包含在缓冲区中的'\n'),您只需将每一行读入足够大小的缓冲区(字符数组)并检查行中的第一个字符是否为'.',并检查第二个字符是否为'\n',如果是,则退出您的读取循环。

    例如:

    #include <stdio.h>
    #include <string.h>
    
    #define MAXC 1024       /* if you need a constant, #define one (or more) */
    
    int main (void) {
        
        char line[MAXC];    /* buffer to hold line */
        
        while (fgets (line, MAXC, stdin)) {         /* read each line */
            /* if 1st char '.' and either EOF or next is '\n', done */
            if (*line == '.' && (!line[1] || line[1] == '\n'))
                break;
            line[strcspn (line, "\n")] = 0;         /* trim \n from end of line */
            puts (line);                            /* output line */
        }
    }
    

    (注意: 以上strcspn (line, "\n") 返回line 中的字符数,直到'\n' 允许您用nul-termianting 覆盖'\n' em> 字符来删除它)

    通常检查以'.' 开头的行中的第二个字符是否为换行符就足够了,但是有许多编辑器不编写符合 POSIX 的文件(意味着在最后一行之后没有'\n')。如果您没有检查 EOF,那么对于具有非 POSIX 文件结尾的文件,如果它出现在最后一行,您将输出最后一个 '.'

    使用/输出示例

    dat/stop_on_dot.txt 中的示例输入重定向到stdin 上的程序作为输入,您将收到以下信息:

    $ ./bin/stop_on_dot < dat/stop_on_dot.txt
    This is a sample text.
    The file will be terminated by a single dot: .
    The program continues processing the lines because the dot (.)
    did not appear at the beginning.
    . even though this line starts with a dot, it is not a single dot.
    The program stops processing lines right here.
    

    查看一下,如果您有任何问题,请告诉我。

    【讨论】:

      【解决方案2】:

      永远不要使用gets,那里没有检查边界。

      至于您的关注,仅使用 getchar() 就足够了,只需检查输入是否为空格字符( \t\n) 我刚刚写了一个小程序来输入直到空格。 我们可以更健壮地编写它,如果这能解决您的问题,请告诉我。

      #include <stdio.h>
      
      int main()
      {
          char ch;
          while(ch = getchar())
          {
              if(ch != ' ' && ch != '\t' && ch != '\n')
                  putchar(ch);
              else
                  break;
          }
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-25
        • 1970-01-01
        相关资源
        最近更新 更多