更改scanf()分隔符 [英] Changing the scanf() delimiter

查看:288
本文介绍了更改scanf()分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将 scanf 的定界符更改为 \n
我尝试使用 scanf(%[^ \n] s,sen); 并适用于单个输入。
但是,当我在 for 循环中将同一行放入多个句子时,会给我垃圾值。

My objective is to change the delimiter of scanf to "\n". I tried using scanf("%[^\n]s",sen); and works fine for single inputs. But when i put the same line inside a for loop for multiple sentences it gives me garbage values.

有人知道为什么吗?

这是我的代码:

char sen[20];
for (i=0;i<2;i++)
{
    scanf("%[^\n]s",sen);
    printf("%s\n",sen);
}


推荐答案

考虑此(C99)代码:

Consider this (C99) code:

#include <stdio.h>

int main(void)
{
    char buffer[256];

    while (scanf("%255[^\n]", buffer) == 1)
        printf("Found <<%s>>\n", buffer);
    int c;
    if ((c = getchar()) != EOF)
        printf("Failed on character %d (%c)\n", c, c);
    return(0);
}

当我运行它并输入字符串时,绝对是 空格 TAB TAB tabs galore!,它给我:

When I run it and type in a string 'absolutely anything with   spaces TABTABtabs galore!', it gives me:

Found <<absolutely anything with   spaces       tabs galore!>>
Failed on character 10 (
)

ASCII(UTF-8)10失败 10 当然是换行符。

ASCII (UTF-8) 1010 is newline, of course.

这是否有助于您理解问题?

Does this help you understand your problem?


在这种情况下(对于单行)它可以工作,但是如果我想将多行输入输入到数组数组中,则会失败。而且我不明白 scanf 如何在您的代码中返回值?

It works in this case (for a single line) but if I want to take multiple lines of input into an array of arrays then it fails. And I don't get how scanf returns a value in your code?

有很多原因使许多(大多数?)经验丰富的C程序员像瘟疫一样避免使用 scanf() fscanf() ;他们太难了,无法正常工作。我建议使用 sscanf()这种替代方法,它不会获得与 scanf() fscanf()做。

There are reasons why many (most?) experienced C programmers avoid scanf() and fscanf() like the plague; they're too hard to get to work correctly. I'd recommend this alternative, using sscanf(), which does not get the same execration that scanf() and fscanf() do.

#include <stdio.h>

int main(void)
{
    char line[256];
    char sen[256];

    while (fgets(line, sizeof(line), stdin) != 0)
    {
        if (sscanf(line, "%255[^\n]", sen) != 1)
            break;
        printf("Found <<%s>>\n", sen);
    }
    int c;
    if ((c = getchar()) != EOF)
        printf("Failed on character %d (%c)\n", c, c);
    return(0);
}

这将读取输入行(使用 fgets( ),确保没有缓冲区溢出(假设您听说过 gets()函数会将您的计算机融为一体。金属和硅),然后使用 sscanf()处理该行。这处理的是换行符,这是原始代码的缺点。

This reads the line of input (using fgets() which ensures no buffer overflow (pretend that the gets() function, if you've heard of it, melts your computer to a pool of metal and silicon), then uses sscanf() to process that line. This deals with newlines, which are the downfall of the original code.

char sen[20];
for (i=0;i<2;i++)
{
    scanf("%[^\n]s",sen);
    printf("%s\n",sen);
}

问题:


  1. 您无需检查 scanf ()成功。

  2. 在第一次迭代中将换行符保留在缓冲区中;第二次迭代生成的返回值为0,因为要读取的第一个字符为

  3. 您看到的胡言乱语很可能是重复的第一行输入,实际上,如果不是针对有限的loo p,它不会等您再输入任何内容;

  1. You do not check whether scanf() succeeded.
  2. You leave the newline in the buffer on the first iteration; the second iteration generates a return value of 0 because the first character to read is newline, which is the character excluded by the scan set.
  3. The gibberish you see is likely the first line of input, repeated. Indeed, if it were not for the bounded loop, it would not wait for you to type anything more; it would spit out the first line over and over again.






scanf()



scanf()的定义(从ISO / IEC 9899:1999中获得)


Return value from scanf()

The definition of scanf() (from ISO/IEC 9899:1999) is:


§7.19.6.4scanf函数

§7.19.6.4 The scanf function

简介

 #include <stdio.h>
 int scanf(const char * restrict format, ...);

说明

2 scanf 函数等效于 fscanf ,参数为 stdin 插入
scanf 的参数之前。

2 The scanf function is equivalent to fscanf with the argument stdin interposed before the arguments to scanf.

返回

3如果在
进行任何转换之前发生输入失败,则 scanf 函数将返回宏EOF的值。否则, scanf 函数将返回分配的输入项目
的数量,该数量可能少于所提供的数量,甚至在$ b较早的情况下为零。 $ b匹配失败。

3 The scanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

请注意,当我的第一个程序中的循环退出时,是因为 scanf( )返回0,而不是EOF。

Note that when the loop in my first program exits, it is because scanf() returned 0, not EOF.

这篇关于更改scanf()分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆