什么scanf函数从输入扫描后删除? [英] What does scanf REMOVES from input after scanning?

查看:304
本文介绍了什么scanf函数从输入扫描后删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了很多的问题和博客和书籍消失了,但我想我只是不能够正确关联的东西。
什么扫描后scanf函数吗?它读取除空格,制表符和换行符一切,但它有什么作用为新行?
我无法扫描后输入任何方式删除换行符。
例如,认为这是输入: -

I have gone through many questions and blogs and books, but I guess I'm just not able to relate things properly. What does scanf do after scanning? It reads everything except space, tab and newline, but what does it do to newline?? I'm unable to remove newline any way after scanning input. For example, consider this as input:-

5
结果ABCDEFGH

5
Abcdefgh

现在,我已经扫描5整数,

Now, I have scanned 5 as integer,

 scanf("%d", &i); 

但我怎么去除,以在提前读到的一切一个字符的'\\ n'?我想用: -

but how do I remove '\n' in order to read everything ahead one character at a time? I want to use:-

 while((c=getchar())!='\n'){//Do something} 

在'\\ n'应该是一个字符串后,却的getchar()得到5一前一后,甚至一度运行前循环终止。我想我只是缺少一个非常小把戏。

The '\n' is supposed to be the one AFTER the string, but getchar() gets the one after 5 and loop terminates before even running once. I guess I'm just missing a very little trick.

scanf("%d", &i);
while((c=getchar())!='\n'){
    //Do something with the character
}

输入文件: - 结果
5结果
ABCDEF
结果
期待while循环运行,虽然它没有运行。由于5之后捕捉的'\\ n'我曾尝试添加在scanf函数之间的getchar()(),虽然(),但后来计划stucks和什么也不做。

Input File:-
5
ABCDEF
Expecting while loop to run, while it doesn't run. Because it catches '\n' after 5. I have tried adding getchar() in between scanf() and while(), but then program stucks and does nothing.

推荐答案

如果您在 scanf函数%d个后的空间code>格式说明, scanf函数将不会返回,直到它遇到的任何非空白字符。这将有效地消耗在输入任意数量的换行符或其他空间。正如@BlueMoon指出,注意,这个依赖在程序中的新行后接受更多的非空白输入。因此,它的工作原理在这个例子中,但可能在其他地方造成麻烦。

If you add a space after the %d in your scanf format specifier, scanf will not return until it encounters any non-whitespace character. This will effectively consume any number of newlines or other spaces in your input. As pointed out by @BlueMoon, note that this relies on the program receiving more non-whitespace input after the newline. So it works for this example but might cause trouble elsewhere.

下面是一个小例子:

#include <stdio.h>

int main()
{
    int i, j=0;
    char c;
    scanf("%d ", &i);
    while((c=getchar())!='\n') printf("%d: %c\n", j++, c);    
    return 0;
}

样本输入:

5   


abcdefg

输出:

0: a
1: b
2: c
3: d
4: e
5: f
6: g


修改:请注意,这个问题可以通过使用与fgets 来规避读行成一个字符缓冲区,然后提取使用整数的sscanf


edit: Note that this issue can be circumvented by using fgets to read a line into a character buffer, then extracting the integer using sscanf:

char buff[SIZE];
fgets(buff, SIZE, stdin);
sscanf(buff, "%d ", &i);
while((c=getchar())!='\n') printf("%d: %c\n", j++, c);

与fgets 读取,直到遇到文件的换行符或结束,或 SIZE-1 字节被读取。假设你只有期待行包含一个整数,这种限制不应该是一个问题。只要尺寸足够大,换行将由与fgets 消耗和随后的在的getchar循环将正常工作。

fgets reads until a newline or end of file is encountered, or SIZE-1 bytes have been read. Assuming you're only expecting the line to contain one integer, this limitation shouldn't be an issue. As long as SIZE is large enough, the newline will be consumed by fgets and the subsequent getchar in the loop will work as expected.

这篇关于什么scanf函数从输入扫描后删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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