笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。

CSDN视频网址:http://edu.csdn.net/lecturer/144

什么是缓冲区?

临时存储区称为缓冲区,所有标准输入和输出设备都包含输入和输出缓冲器。在标准C / C ++中,流是缓冲的,例如在标准输入的情况下,

当我们按键盘上的键时,它不会发送到您的程序,而是由操作系统缓冲,直到时间被分配给程序。

它如何影响编程?

在各种情况下,您可能需要清除不需要的缓冲区,以便获得所需容器中的下一个输入,而不是在前一个变量的缓冲区中。例如,
在遇到“scanf()”之后的C的情况下,如果我们需要输入一个字符数组或字符,并且在C ++的情况下,遇到“cin”语句之后,我们需要输入一
个字符数组或一个字符串,我们需要清除输入缓冲区,否则所需的输入由前一个变量的缓冲区占用,而不是所需的容器。在第一次输入后按
输出屏幕上的“回车”(回车),前一个变量的缓冲区为新容器的空间(因为我们没有清除它),程序跳过容器的以下输入。

在C编程的情况下

#include<stdio.h>
int main()
{
    char str[80], ch;

    // Scan input from user -TestGame for example
    scanf("%s", str);

    // Scan character from user- 'a' for example
    ch = getchar();

    // Printing character array, prints “TestGame”)
    printf("%s\n", str);

    // This does not print character 'a'
    printf("%c", ch);

    return 0;
}

输入:

TestGame

一个

输出:

TestGame

在C ++的情况下

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int a;
    char ch[80];

    // Enter input from user - 4 for example
    cin >> a;

    // Get input from user - "TestGame" for example
    cin.getline(ch,80);

    // Prints 4
    cout << a << endl;

    // Printing string : This does not print string
    cout << ch << endl;

    return 0;
}

输入:

4
TestGame

输出:

4

在上述两种代码中,都不会根据需要打印输出。原因是被占用的缓冲区。“\ n”字符保留在缓冲区中,并作为下一个输入读取。

如何解决?

在C:

1、使用“while((getchar())!='\ n'); “(键入)while((getchar())!='\ n');”读缓冲区字符直到结束并丢弃它们(包括换行符),

并在“scanf()”语句清除输入缓冲区之后使用它允许输入到所需的容器中。

#include<stdio.h>
int main()
{
    char str[80], ch;

    // scan input from user - TestGame for example
    scanf("%s", str);

    // flushes the standard input (clears the input buffer)
    while ((getchar()) != '\n');
     
    // scan character from user - 'a' for example
    ch = getchar();

    // Printing character array, prints “TestGame”)
    printf("%s\n", str);

    // Printing character a: It will print 'a' this time
    printf("%c", ch);

    return 0;
}

输入:

TestGame
一个

输出:

TestGame
一个

2、使用“fflush(stdin)”:在“scanf()”语句之后键入“fflush(stdin)”也会清除输入缓冲区,但是避免使用它,并且根据C ++被称为输入流“未定义” 11标准。

在C ++的情况下:

1、使用“cin.ignore(numeric_limits :: max(),'\ n'); “: - 在”cin“语句之后输入”cin.ignore(numeric_limits :: max(),'\ n');“后,放弃包含换行符的输入流中的所有内容。
#include<iostream>
#include<ios>     // for <streamsize>
#include<limits>  // for numeric_limits
using namespace std;
int main()
{
    int a;
    char str[80];

    // Enter input from user - 4 for example
    cin >> a;

    // discards the input buffer
    cin.ignore(numeric_limits<streamsize>::max(),'\n');

    // Get input from user - TestGame for example
    cin.getline(str, 80);

    // Prints 4
    cout << a << endl;

    // Printing string : This will print string now
    cout << str << endl;

    return 0;
}

输入:

4
TestGame

输出:

4
TestGame

2、使用“cin.sync()”:在“cin”语句之后键入“cin.sync()”将放弃缓冲区中的所有内容。虽然“cin.sync()” 不工作在所有实施(根据C ++ 11和上述标准)。

#include<iostream>
#include<ios>
#include<limits>
using namespace std;
int main()
{
    int a;
    char str[80];

    // Enter input from user - 4 for example
    cin >> a;

    // Discards the input buffer
    cin.sync();

    // Get input from user - TestGame for example
    cin.getline(str, 80);

    // Prints 4
    cout << a << endl;

    // Printing string - this will print string now
    cout << str << endl;

    return 0;
}

输入:

4
TestGame

输出:

4
TestGame

使用“cin >> ws”:在“cin”语句之后键入“cin >> ws”,告诉编译器忽略缓冲区,并且在字符串或字符数组的实际内容之前丢弃所有的空格。

在C / C ++中清除输入缓冲区的更多相关文章

  1. scanf()中清除输入缓冲区的几种方法归纳

    应用场景:我们使用多个scanf()的时候,如果输入缓冲区还有数据的话,那么scanf()就不会询问用户输入,而是直接就将输入缓冲区的内容拿出来用了,这就导致了前面的错误影响到后面的内容,为了隔离这种 ...

  2. c++清除输入缓冲区之 sync() vs ignore()

    最近在写程序的时候总是不注意输入缓冲区内是否还有东西,导致出现了一些异常,调试了半天.所以来上一贴,学习注意,引以为戒! http://blog.chinaunix.net/uid-21254310- ...

  3. c++中清空输入缓冲区的方法(做cf的时候炸了)

    C/C++ 四种清空输入缓冲区的方法 比较实用的一种 char c; while(c=getchar()!='\n'); 或者是这种 cin.ignore(count,c); count代表要清除的字 ...

  4. C语言清空输入缓冲区的N种方法对比

    转自C语言清空输入缓冲区的N种方法对比 C语言中有几个基本输入函数: //获取字符系列 int fgetc(FILE *stream); int getc(FILE *stream); int get ...

  5. C语言清空输入缓冲区的N种方法对比【转】

    转自:http://www.cnblogs.com/codingmylife/archive/2010/04/18/1714954.html C语言中有几个基本输入函数: //获取字符系列 int f ...

  6. [转][修]C清空输入缓冲区

    为何要清空输入缓存区     读取时输入缓冲区中的内容会被scanf函数逐个取走,正常case下scanf()函数可以根据返回值判断成功取走的数目:但当发生读取异常之后,输入缓冲区中的内容并未被取走, ...

  7. C语言清空输入缓冲区的N种方法对比(转)

    C语言中有几个基本输入函数: //获取字符系列 int fgetc(FILE *stream); int getc(FILE *stream); int getchar(void); //获取行系列 ...

  8. C 清空输入缓冲区,以及fflush(stdin)的使用误区和解决方法

    转载:https://blog.csdn.net/Veniversum/article/details/62048870 对C 语言初学者来说,fflush(stdin)函数被解释为会清空输入缓冲区的 ...

  9. C++重载输入和输出操作符以及IO标准库中的刷新输入缓冲区残留字符问题

    今天在做C++ Primer习题的14.11时,印象中应该挺简单的一题,结果却费了很长时间. 类定义: typedef string Date; class CheckoutRecord{ publi ...

随机推荐

  1. 动态更改WebBrowser数据流内容

    动态更改WebBrowser数据流内容       有时,由于软件的特殊需要,我们希望DELPHI在WebBrowser或embeddedwb里动态更改返回的数据内容,而这需要返回网页的所有原始源码, ...

  2. freemarker split字符串分割 遍历map

    <#list "张三三,李思思,,王强,柳树,诸葛正我"?split(",") as name> "${name}" </ ...

  3. 搭建Mac OS X下cocos2d-x的Android开发环境

    版本 Cocos2d-x: cocos2d-2.1beta3-x-2.1.1 OS X: 10.8 Android ADT Bundle: v21.1.0 Android NDK: android-n ...

  4. application/x-www-form-urlencoded和multipart/form-data

     我们在提交表单的时候,form表单参数中会有一个enctype的参数. EncType表明提交数据的格式,用 Enctype 属性指定将数据发到服务器时浏览器使用的编码类型. enctype指定了H ...

  5. ORM实例介绍

    http://blog.csdn.net/RonoTian/article/details/2900714

  6. Qt5.5.1移植到freescale imx6

    一.环境 HOST:ubuntu12.04-LTS Embedded:freescale imx6 linux-3.0.35 CROSS_COMPILE:freescale提供的gcc-4.6.2-g ...

  7. [Android Studio系列(五)] Android Studio手动配置Gradle的方法

    1 问题 (1) android sutdio第一次打开一个工程巨慢怎么办? (2) 手动配置Gradle Home为什么总是无效? (3) 明明已经下载了Gradle,配置了gradle home, ...

  8. Spring_管理 Bean 的生命周期

    beans-cycle.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns=&quo ...

  9. hibernate-Table 'XXX.XXX' doesn't exist

    hibernate---Table 'XXX.XXX' doesn't exist 在设置自动生成数据表的策略中: <!-- 自动生成数据表的策略 --> <property nam ...

  10. Web Service简介

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...