C primer plus自用知识点整理(第八章)字符输入输出函数、输入验证(混合输入)
本书假设所有的输入都是缓冲输入
发布日期:2022-01-20 01:06:50
浏览次数:2
分类:技术文章
本文共 4899 字,大约阅读时间需要 16 分钟。
最近在看C primer plus(加深巩固自己的C语言技巧,为以后学习C++打个基础)。
里面知识针对自己以后要查的点整理出来。 使用工具:visual studio 2013 :https://blog.csdn.net/answerMack/article/details/103766020 :https://blog.csdn.net/answerMack/article/details/103805900 :https://blog.csdn.net/answerMack/article/details/103855794 :https://blog.csdn.net/answerMack/article/details/103870182 https://blog.csdn.net/answerMack/article/details/103891048目录
主要介绍输入\输出函数(I/O函数):
输入函数:scanf()、getchar(); 输出函数:printf()、putchar();getchar()、putchar();
为单字符IO,每次只处理一个字符。也是绝大多数文本(即,普通文字)处理程序所用的核心方法。与头文件stdio.h相关联。不是真正的函数,被定义为供预处理器使用的宏。详解在16章。
C程序如何处理键盘输入,尤其是缓冲和标准输入文件的概念。缓冲区
为什么要有缓冲?
1、把若干字符作为一个块传输比逐个发送字符节约时间 2、若打错字符,可直接通过键盘修正错误。当按下Enter时,传输的是正确的输入。本书假设所有的输入都是缓冲输入
结束键盘输入
从文件开始学习如何阶数文件: 结果:重定向和文件
Unix系统重定向
系统重定向输入:符号为“<”
win10系统重定向: 在win10的cmd命令行中执行: 打印出word.txt文件内容: 系统重定向输出:符号为“>” 结果: 覆盖掉之前的内容,替换了新的内容: 组合重定向: 重定向运算符原则: 符号“>>”和" | "用程序直接打开文件
fopen等函数。具体介绍见后面几章。
fopen和fopen_s用法的比较
fopen 和 fopen_s fopen用法: fp = fopen(filename,“w”)。 fopen_s用法:,须定义另外一个变量errno_t err,然后err = fopen_s(&fp,filename,“w”)。 返回值: fopen打开文件成功,返回文件指针(赋值给fp),打开失败则返回NULL值; fopen_s打开文件成功返回0,失败返回非0。 在定义FILE * fp 之后,fopen的用法是: fp = fopen(filename,“w”)。而对于fopen_s来说,还得定义另外一个变量errno_t err,然后err = fopen_s(&fp,filename,“w”)。返回值的话,对于fopen来说,打开文件成功的话返回文件指针(赋值给fp),打开失败则返回NULL值;对于fopen_s来说,打开文件成功返回0,失败返回非0。缓冲输入
混合数值和字符输入
程序 结果:输入验证
// checking.c -- validating input#include#include // validate that input is an integerlong get_long(void);// validate that range limits are validbool bad_limits(long begin, long end, long low, long high);// calculate the sum of the squares of the integers// a through bdouble sum_squares(long a, long b);int main(void){ const long MIN = -10000000L; // lower limit to range const long MAX = +10000000L; // upper limit to range long start; // start of range long stop; // end of range double answer; printf("This program computes the sum of the squares of " "integers in a range.\nThe lower bound should not " "be less than -10000000 and\nthe upper bound " "should not be more than +10000000.\nEnter the " "limits (enter 0 for both limits to quit):\n" ); printf("lower limit: "); start = get_long(); printf("upper limit: "); stop = get_long(); while (start != 0 || stop != 0) { if (bad_limits(start, stop, MIN, MAX)) printf("Please try again.\n"); else { answer = sum_squares(start, stop); printf("The sum of the squares of the integers "); printf("from %ld to %ld is %g\n", start, stop, answer); } printf("Enter the limits (enter 0 for both " "limits to quit):\n"); printf("lower limit: "); start = get_long(); printf("upper limit: "); stop = get_long(); } printf("Done.\n"); getchar(); return 0;}long get_long(void){ long input; char ch; while (scanf_s("%ld", &input) != 1) { while ((ch = getchar()) != '\n') putchar(ch); // dispose of bad input printf(" is not an integer.\nPlease enter an "); printf("integer value, such as 25, -178, or 3: "); } return input;}double sum_squares(long a, long b){ double total = 0; long i; for (i = a; i <= b; i++) total += (double)i * (double)i; return total;}bool bad_limits(long begin, long end, long low, long high){ bool not_good = false; if (begin > end) { printf("%ld isn't smaller than %ld.\n", begin, end); not_good = true; } if (begin < low || end < low) { printf("Values must be %ld or greater.\n", low); not_good = true; } if (begin > high || end > high) { printf("Values must be %ld or less.\n", high); not_good = true; } return not_good;}
菜单浏览
/* menuette.c -- menu techniques */#includechar get_choice(void);char get_first(void);int get_int(void);void count(void);int main(void){ int choice; void count(void); while ((choice = get_choice()) != 'q') { switch (choice) { case 'a': printf("Buy low, sell high.\n"); break; case 'b': putchar('\a'); /* ANSI */ break; case 'c': count(); break; default: printf("Program error!\n"); break; } } printf("Bye.\n"); return 0;}void count(void){ int n, i; printf("Count how far? Enter an integer:\n"); n = get_int(); for (i = 1; i <= n; i++) printf("%d\n", i); while (getchar() != '\n') continue;}char get_choice(void){ int ch; printf("Enter the letter of your choice:\n"); printf("a. advice b. bell\n"); printf("c. count q. quit\n"); ch = get_first(); while ((ch < 'a' || ch > 'c') && ch != 'q') { printf("Please respond with a, b, c, or q.\n"); ch = get_first(); } return ch;}char get_first(void){ int ch; ch = getchar(); while (getchar() != '\n') continue; return ch;}int get_int(void){ int input; char ch; while (scanf_s("%d", &input) != 1) { while ((ch = getchar()) != '\n') putchar(ch); // dispose of bad input printf(" is not an integer.\nPlease enter an "); printf("integer value, such as 25, -178, or 3: "); } return input;}
本章小结
转载地址:https://blog.csdn.net/answerMack/article/details/103953376 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年03月23日 08时21分06秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
安卓界面——最开始界面的加载
2019-04-26
安卓——屏蔽陌生来电
2019-04-26
安卓——小笔记
2019-04-26
客户端面试万金油
2019-04-26
【u3d泰斗破坏神】05 --- 角色移动 velocity 的相关问题
2019-04-26
【u3d泰斗破坏神】06 --- Loading界面进度条Slider的使用
2019-04-26
【u3d泰斗破坏神】07 --- 角色攻击动画拆分、状态机设计
2019-04-26
【u3d泰斗破坏神】08 --- UGUI 制作艺术字体
2019-04-26
【u3d泰斗破坏神】09 --- 角色血条的制作、掉血特效
2019-04-26
Unity Shader 入门精要(01) -- 渲染流水线
2019-04-26
Unity Shader 入门精要(02) -- shader的编码基础
2019-04-26
Unity Shader 入门精要(03) -- Unity的基础光照
2019-04-26
Unity Shader 入门精要(04) -- 基础纹理
2019-04-26
Unity3D 移动平台的资源路径问题
2019-04-26
二分查找(折半查找)
2019-04-26
线段树
2019-04-26
编程机制
2019-04-26
自己写的Java版计算器
2019-04-26
字、位、字节摘抄的,怕忘了
2019-04-26
printf与scanf的用法知识(C Primer Plus总结)
2019-04-26