#include<stdio.h>

int main() {
char str[];
while () {
printf("Please input:\n");
int rtn=scanf_s("%s", str, );
if (rtn == ) {
printf("scanf_s failure!\n");
rewind(stdin);//windows平台下清空字符缓冲区。fflush(stdin)不建议使用,好像没有效果
}
else {
printf("Your input:%s\n", str);
}
printf("%d-----------------------------------------------\n", rtn);
}
return ;
}

如果是使用scanf注意内存越界,改变了其他变量的值。注意限制字符读取的长度。

以下是运行结果:

Please input:

scanf_s failure!
-----------------------------------------------
Please input: Your input:
-----------------------------------------------
Please input: Your input:
-----------------------------------------------
Please input: Your input:
-----------------------------------------------
Please input: Your input:
-----------------------------------------------
Please input: scanf_s failure!
-----------------------------------------------
Please input: scanf_s failure!
-----------------------------------------------
Please input:

如果清空缓冲区的那一行代码被注释,如下:

#include<stdio.h>

int main() {
char str[];
while () {
printf("Please input:\n");
int rtn=scanf_s("%s", str, );
if (rtn == ) {
printf("scanf_s failure!\n");
//rewind(stdin);//windows平台下清空字符缓冲区。fflush(stdin)不建议使用,好像没有效果
}
else {
printf("Your input:%s\n", str);
}
printf("%d-----------------------------------------------\n", rtn);
}
return ;
}

则运行结果变成了

Please input:

scanf_s failure!
-----------------------------------------------
Please input:
Your input:
-----------------------------------------------
Please input: scanf_s failure!
-----------------------------------------------
Please input:
scanf_s failure!
-----------------------------------------------
Please input:
scanf_s failure!
-----------------------------------------------
Please input:
Your input:
-----------------------------------------------
Please input:

最后一定要注意scanf_s的后面的长度虽然是5,但是只能接收4个字符,不然就会接收键盘输入失败。

另外,scanf_s的第二个参数是变量的地址(数组名也是变量的地址),第三个参数是(unsigned int)的类型(也可以直接使用正整数)

scanf_s获取参数,清空缓冲区,判断是否读取成功的更多相关文章

  1. C++中清空缓冲区

    C++中标准输入cin有多种输入方式.. 这篇文章罗列的还是简要易懂的.C++输入cin详解...如果只是简单的使用cin>>的话,会单个token的读入.但是会忽略换行符,空格,制表符等 ...

  2. Onvif开发之客户端鉴权获取参数篇

    前面一篇已经介绍了客户端如何发些设备,下面这篇简单介绍下在发现设备后,如何通过ONVIF协议来获取设备的相关参数 ONVIF中不管是客户端还是设备端,最先实现的接口都是关于能力的那个接口,在客户端实现 ...

  3. C语言:清空缓冲区

    缓冲区的优点很明显,它加快了程序的运行速度,减少了硬件的读写次数,让整个计算机变得流畅起来:但是,缓冲区也带来了一些负面影响,经过前面几节的学习相信读者也见识到了.那么,该如何消除这些负面影响呢?思路 ...

  4. WebApi Post 后台无法获取参数的解决方案

    事件回放: 之前一段时间,公司里前端用的Angularjs 发送http请求也是用的ng的组件,后台是.Net的WebApi 前端 var data = { PArgs: { PageIndex: 0 ...

  5. 同样有缓冲区,为什么bufferedReader输入流不需要清空缓冲区?而bufferedWriter需要清空缓冲区呢?

    当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并置入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取, 如果缓冲区数据不足,才会再从文件中读取.清不清空Buf ...

  6. phalcon: 获取参数的方法

    phalcon: 获取参数的方法 一般情况下:GET/POST $this->request->get(参数); $this->request->getPost("参 ...

  7. lua post参数获取,参数截断

    post 请求头: a.application/x-www-form-urlencoded 普通表单提交 b.multipart/form-data 含有文件的表单,二进制上传 c.applicati ...

  8. JSP基础知识➣获取参数和过滤器(四)

    JSP表单提交和参数获取 JSP表单提交的两种方式:post和get,通过这两种方式提交的参数到后台,获取参数的值主要由request来处理,获取值的方式有以下几种: getParameter():  ...

  9. python中readline判断文件读取结束的方法

    注:内容来自网络 本文实例讲述了python中readline判断文件读取结束的方法.分享给大家供大家参考.具体分析如下: 大家知道,python中按行读取文件可以使用readline函数,下面现介绍 ...

随机推荐

  1. Thrift 入门

    1下载 https://thrift.apache.org/download 或 Apache Thrift Archive 下载编译好的release可执行文件: Thrift compiler f ...

  2. tomcat复习

    javaEE 服务器: java企业版服务器:是一个标准 BS/CS: 1> CS: client/Server 优点:易操作性比较高 缺点:客户端需要升级维护. 2> BS: Brows ...

  3. class JsonItemExporter(BaseItemExporter):

    class JsonItemExporter(BaseItemExporter):这个类的实现和几年前的实现有了点小变化,主要就是是否添加换行

  4. Codeforces Round #402 (Div. 2) B

    Description Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k. ...

  5. 洛谷 P1892 团伙

    P1892 团伙 并查集 #include<cstdio> int fa[2500];//fa[i]表示i的朋友所在集合,fa[i+n]表示i的敌人所在集合 bool boo[2500]; ...

  6. 线段树(单点更新) HDOJ 4288 Coder

    题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...

  7. 线段树(单点更新) POJ 2828 Buy tickets

    题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ...

  8. iOS7改变状态栏文字颜色

    1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的 AppDelegate中在 didFinishLaun ...

  9. HAL之PWM

    PWM是定时器的一个输出功能,要分配在有对应输出的管脚上.分频和定时值决定了周期,捕获寄存器的值就是占空比,当计数寄存器的值小于捕获值时输出固定电平(H),当大于时翻转电平,当计数器值溢出时将重载值载 ...

  10. Windows下降权MYSQL和apche的运行级别(普通用户权限运行)

    1.MYSQL的降权运行  新建立一个用户比如mysql  net user mysql microsoft /add  net localgroup users mysql /del  不属于任何组 ...