我是靠谱客的博主 精明镜子,这篇文章主要介绍c primer plus第8章总结:字符输入输出,现在分享给大家,希望可以做个参考。

1、缓冲:
     完全缓冲:区满时清空;常见为512字或4096字;
     行缓冲:遇到 n 清空;

2、重定向:
      用文件代替键盘和屏幕;
     将输出重定向到一个文件:>        prog > file1
     将输入重定向为来自一个文件夹:<        prog < file2

3、混合输入:
     scanf() 会在遇到第一个空格、制表符,换行符时,会停止读取;
     getchar() 会读取输入的每一个字符;
      
# 只允许输入整数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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; }

# 只允许float类型输入,排除字母类输入:
复制代码
1
2
3
scanf("%lf", &temps); while(temps>='a'&&temps<'z'||temps>='A'&&temps<='Z') break;
     
             # 用get_first() 代替 getchar() ,使程序运行更顺畅,跳过换行符,避免换行符作为错误响应对待;   
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char get_choice(void) { int ch; printf("Enter the letter of your choice:n"); printf("a. advice b. belln"); printf("c. count q. quitn"); 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() 出现不希望的动作
复制代码
1
2
3
4
5
6
7
8
9
10
char get_first(void) { int ch; ch = getchar(); // 读取遇到的第一个字符,赋值给ch;使用两次getchar(),最终return ch while (getchar() != 'n') continue; // 跳过本行的剩余部分 return ch; }

# 获取输入的第一个非空字符,并跳过输入的剩余部分
复制代码
1
2
3
4
5
6
7
8
9
10
11
char get_unspace_first(void) { int ch; while ( isspace(ch = getchar())) continue; // 获取第一个非空字符,赋值给ch while (getchar() != 'n') continue; // 跳过本行的剩余部分 return ch; }

# 只允许输入一个字符
复制代码
1
2
3
4
5
6
7
8
9
10
11
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
2
3
4
5
6
7
8
9
10
11
12
13
#限制输入为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; }



课后习题
# 第1题 统计从文件输入到文件结尾的字符数;
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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的某个数
            
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 用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"); }



最后

以上就是精明镜子最近收集整理的关于c primer plus第8章总结:字符输入输出的全部内容,更多相关c内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部