c primer plus第8章总结:字符输入输出
2016-06-16 19:39
441 查看
1、缓冲:
完全缓冲:区满时清空;常见为512字或4096字;
行缓冲:遇到 \n 清空;
2、重定向:
用文件代替键盘和屏幕;
将输出重定向到一个文件:> prog > file1
将输入重定向为来自一个文件夹:< prog < file2
3、混合输入:
scanf() 会在遇到第一个空格、制表符,换行符时,会停止读取;
getchar() 会读取输入的每一个字符;
[align=justify]# 只允许输入整数[/align]
[align=left]# 只允许float类型输入,排除字母类输入:[/align]
# 用get_first() 代替 getchar() ,使程序运行更顺畅,跳过换行符,避免换行符作为错误响应对待;
# 获取输入的第一个字符;(注意第一个字符有可能是\n,会导致get_choice() 出现不希望的动作)
# 获取输入的第一个非空字符,并跳过输入的剩余部分
[align=justify]# 只允许输入一个字符[/align]
[align=justify]课后习题[/align]
[align=justify]# 第1题 统计从文件输入到文件结尾的字符数;[/align]
# 第5题 用二分搜索法猜测用户所想的0-100的某个数
完全缓冲:区满时清空;常见为512字或4096字;
行缓冲:遇到 \n 清空;
2、重定向:
用文件代替键盘和屏幕;
将输出重定向到一个文件:> prog > file1
将输入重定向为来自一个文件夹:< prog < file2
3、混合输入:
scanf() 会在遇到第一个空格、制表符,换行符时,会停止读取;
getchar() 会读取输入的每一个字符;
[align=justify]# 只允许输入整数[/align]
int get_int(void) { int input; char ch; while (scanf("%d", &input) != 1) // 用!=来判断输入更友好,给用户重新输入的机会; { while ((ch = getchar()) != '\n') putchar(ch); // 剔除错误输入; printf(" is not an integer.\nPlease enter an integer value, such as 25, -178, or 3: "); } return input; }
[align=left]# 只允许float类型输入,排除字母类输入:[/align]
scanf("%lf", &temps); while(temps>='a'&&temps<'z'||temps>='A'&&temps<='Z') break;
# 用get_first() 代替 getchar() ,使程序运行更顺畅,跳过换行符,避免换行符作为错误响应对待;
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; }
# 获取输入的第一个字符;(注意第一个字符有可能是\n,会导致get_choice() 出现不希望的动作)
char get_first(void) { int ch; ch = getchar(); // 读取遇到的第一个字符,赋值给ch;使用两次getchar(),最终return ch while (getchar() != '\n') continue; // 跳过本行的剩余部分 return ch; }
# 获取输入的第一个非空字符,并跳过输入的剩余部分
char get_unspace_first(void) { int ch; while ( isspace(ch = getchar())) continue; // 获取第一个非空字符,赋值给ch while (getchar() != '\n') continue; // 跳过本行的剩余部分 return ch; }
[align=justify]# 只允许输入一个字符[/align]
int getOneChar() { char ch = 0, ret = 0; int count = 0; while (scanf("%c", &ch) == 1 && ch != '\n') { ret = ch; ++count; } return (count > 1) ? -1 : ret; }
#限制输入为1-5的整数。 while ((status = scanf("%d", &code)) != 1 || (code < 1 || code > 5)) // 跳过非数字输入 { if (status != 1) scanf("%*s"); // 跳至下一个空白字符 printf("Enter an integer from 1 to 5, please.\n"); } return code; }
[align=justify]课后习题[/align]
[align=justify]# 第1题 统计从文件输入到文件结尾的字符数;[/align]
void proj_1() { int ch = 0; FILE * fp; char fname[50]; // 保存文件名 printf("Enter the name of the file: \n"); while (scanf("%s", fname) == 1) // 输入的文件名需要带路径,例 e:\\aa.txt { int count = 0; fp = fopen(fname, "r"); if (fp == NULL) { printf("Failed to open file.please input right file name:\n"); continue; } while ((ch = getc(fp)) != EOF) // EOF 文件终止符,部分电脑可用Ctrl+z 来模拟EOF { ++count; putchar(ch); } fclose(fp); printf("\nAttention:%s file have %d chars.\n",fname, count); } }
# 第5题 用二分搜索法猜测用户所想的0-100的某个数
// 用guess给 min 和 max 动态赋值,逐渐缩小猜测范围; void proj_5() { int guess = 50, min = 0, max = 100; char ch = 0; printf("pick an integer from 1 to 100.I will try to guess it.\n"); printf("Respond with a 'y' if my guess is right.'l' if my guess " "is larger than the number , with an 's' for smaller than.\n"); printf("is your number %d?\n", guess); while ((ch = getchar()) != 'y') { if (ch == 'l') { max = guess; guess = ( min + max )/ 2; printf("then,it is %d?\n", guess); } else if (ch == 's') { min = guess; guess = ( min + max )/ 2; printf("then,it is %d?\n", guess); } else printf("sorry i only unstand y , l or s.\n"); while (getchar()!= '\n') continue; } printf("I know i can do it!\n"); }
相关文章推荐
- GetChar缓存机制深入剖析
- c语言中getch,getche,getchar的区别
- 在输入输出字符串时scanf(),printf()和gets(),puts()的区别浅谈
- 浅析C语言中printf(),sprintf(),scanf(),sscanf()的用法和区别
- 详解C语言中scanf函数使用的一些注意点
- 使用scanf询问用户是否继续
- C语言中几个常用输入函数的异同点
- 由scanf说起之1:scanf函数和回车、空格 及其返回值
- getchar( ) getch( ) getche( )
- scanf&getchar&gets及一些特殊用法
- C语言#getchar()函数
- C语言基础-数据类型提升的层次结构
- APUE课后习题10.6练习代码
- 黑马程序员——C语言基础知识整理——printf函数与scanf函数
- [零基础学软件开发7]输入数据scanf及顺序结构
- 黑马程序员——C语言笔记之scanf函数及使用注意事项
- [C语言] 对于scanf()函数使用%d格式说明符无法读取字符的个人理解
- 字符串相关函数使用之一
- 那个虐了我的杭电1000
- scanf()函数慎重使用