#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. git status -s 的表达式的读法

     1  2  读法  解决方案  ??    未被追踪  git add -A 或git add 文件路径  A    新添加文件 注:??被add后的状态  OK,等待commit.  M    已 ...

  2. hdoj5835【水题】

    思路:不想说了..具体看代码... #include <iostream> #include <stdio.h> #include <string.h> #incl ...

  3. 【水水水】678A - Johny Likes Numbers

    #include<stdio.h> #include<iostream> #include<cstdio> #include<queue> #inclu ...

  4. 基于FBX SDK的FBX模型解析与加载 -(二)

    http://blog.csdn.net/bugrunner/article/details/7211515 5. 加载材质 Material是一个模型渲染时必不可少的部分,当然,这些信息也被存到了F ...

  5. Python解释器的安装步骤

    Python是一门强大的语言,目前已支持所有主流操作系统,在Linux,Unix,Mac系统上自带Python环境,在Windows10系统上需要安装一下,超简单 1.  打开官网 https://w ...

  6. poj 2412 The Balance 【exgcd】By cellur925

    题目传送门 一遇到数学就卡住,我这是怎么肥4...(或许到图论会愉悦吧,逃) Description * 给出两种重量为的 A, B 的砝码,给出一种使用最少的砝码的方式,称出重量 C. 我们可以比较 ...

  7. 交表(Send a Table)

    #include<stdio.h> #include<string.h> #define N 50010 int phi[N],n,sum[N]; void phi_table ...

  8. Codeforces 1144F(二分染色)

    发现奇环不可行,偶环可行,考虑二分图.然后染色,方向全都从一种指向另一种就可以了,随意. ; int n, m, color[maxn]; vector<int> vc[maxn]; ve ...

  9. 144 Binary Tree Preorder Traversal 二叉树的前序遍历

    给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3],   1    \     2    /   3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...

  10. javaFx 学习笔记

    1.每个javaFx程序定义在一个继承自javafx.application.Application的类中 Button:用于设置一个按钮,Button btOK = new Button(" ...