【《C Primer Plus》读书笔记】第8章:字符输入/输出和输入验证
8.1 单字符I/O
getchar() 和 putchar() 每次只处理一个字符。
echo.c 程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
{
putchar(ch);
}
system("pause");
return 0;
}
运行结果:
8.2 缓冲区
概念
缓冲区又称为缓存,它是内存空间的一部分。也就是说,在内存空间中预留了一定的存储空间,这些存储空间用来缓冲输入或输出的数据,这部分预留的空间就叫做缓冲区。
缓冲区根据其对应的是输入设备还是输出设备,分为输入缓冲区和输出缓冲区。
为什么要引入缓冲区?
比如我们从磁盘里取信息,我们先把读出的数据放在缓冲区,计算机再直接从缓冲区中取数据,等缓冲区的数据取完后再去磁盘中读取,这样就可以减少磁盘的读写次数,再加上计算机对缓冲区的操作大大快于对磁盘的操作,故应用缓冲区可大大提高计算机的运行速度。
又比如,我们使用打印机打印文档,由于打印机的打印速度相对较慢,我们先把文档输出到打印机相应的缓冲区,打印机再自行逐步打印,这时我们的CPU可以处理别的事情。
缓冲区就是一块内存区,它用在输入输出设备和CPU之间,用来缓存数据。它使得低速的输入输出设备和高速的CPU能够协调工作,避免低速的输入输出设备占用CPU,解放出CPU,使其能够高效率工作。
缓冲区的类型
-
全缓冲
在这种情况下,当填满标准I/O缓存后才刷新缓冲区,进行实际I/O操作(内容被发送至目的地)。全缓冲的典型代表是对磁盘文件的读写。缓冲区的大小取决于系统,常见的大小是512bytes和4096bytes。 -
行缓冲
在这种情况下,当在输入和输出中遇到换行符时,执行真正的I/O操作。这时,我们输入的字符先存放在缓冲区,等按下回车键换行时才进行实际的I/O操作。典型代表是标准输入(stdin)和标准输出(stdout)。 -
不带缓冲
也就是不进行缓冲,标准出错情况stderr是典型代表,这使得出错信息可以直接尽快地显示出来。
从8.1节echo.c 程序的运行结果来看,程序使用的缓冲是行缓冲。键盘输入通常是行缓冲输入,所以在按下Enter键后才刷新缓冲区。
ANSI C
ANSI C( C89 )要求缓存具有下列特征:
- 当且仅当标准输入和标准输出并不涉及交互设备时,它们才是全缓存的。
- 标准出错决不会是全缓存的。
但是,这并没有告诉我们如果标准输入和输出涉及交互作用设备时,它们是不带缓存的还是行缓存的,以及标准输出是不带缓存的,还是行缓存的。
大部分系统默认使用下列类型的缓存:
- 标准出错是不带缓存的。
- 如果是涉及终端设备的流,则它们是行缓存的;否则是全缓存的。
我们经常要用到标准输入输出流,而ANSI C对stdin、stdout和stderr的缓存特征没有强行的规定,以至于不同的系统可能有不同的stdin、stdout和stderr的缓存特征。目前主要的缓存特征是:stdin和stdout是行缓存;而stderr是无缓存的。
缓冲区的大小
如果我们没有自己设置缓冲区的话,系统会默认为标准输入输出设置一个缓冲区,这个缓冲区的大小通常是4096个字节的大小,这和计算机中的分页机制有关,因为进程在计算机中分配内存使用的就是分页与分段的机制,并且每个页的大小是4096个字节,因此通常情况下缓冲区的大小会设置为4096个字节的大小。
缓冲区的刷新(清空)
下列情况会引发缓冲区的刷新:
- 缓冲区满时;
- 行缓冲区遇到回车时;
- 关闭文件;
- 使用特定函数刷新缓冲区。
8.3 文件、流
文件是存储器中存储信息的区域。
C 语言把所有的设备都当作文件。所以设备(比如显示器)被处理的方式与文件相同。以下三个文件会在程序执行时自动打开,以便访问键盘和屏幕。
文件指针是访问文件的方式,本节将讲解如何从屏幕读取值以及如何把结果输出到屏幕上。
C 语言中的 I/O (输入/输出) 通常使用 printf() 和 scanf() 两个函数。
scanf() 函数用于从标准输入(键盘)读取并格式化, printf() 函数发送格式化输出到标准输出(屏幕)。
从概念上看,C 程序处理的是流而不是直接处理文件。流是一个实际输入或输出映射的理想化数据流。这意味着不同属性和不同种类的输入,由属性更统一的流来表示。
文件结尾
检测文件结尾的一种方法是,在文件末尾放一个特殊的字符标记文件结尾。
这个特殊的字符标记一般是:Ctrl+Z。
在下面的程序中,要结束输入,在按下 Ctrl+Z后,程序会就结束。
echo_eof.c 程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch;
while ((ch = getchar()) != EOF)
{
putchar(ch);
}
system("pause");
return 0;
}
运行结果:
8.4 重定向和文件
在默认情况下,C 程序使用标准 I/O 包查找标准输入作为输入源,这就是 stdin 流。
程序可以通过2种方式使用文件:
- 显式使用特定的函数打开、关闭、读取、写入文件。
- 设计能与键盘和屏幕互动的程序,通过不同的渠道重定向输入至文件和从文件输出。
重定向输入让程序使用文件而不是键盘输入。
重定向输出让程序输出至文件而不是屏幕。
输入重定向
输入重定向即用文本文件代替键盘当作程序的输入。 ‘ < ‘ 符号是Unix、Linux和DOS的重定向运算符。该运算把文件和stdin流关联起来,将该文件的内容引导至程序。程序本身并不知道(或关心)输入是来自文件而不是来自键盘。C将文件和I/O设备置于相同的地位,所以现在这个文件就是I/O设备。我们来写个程序感受一下到底是怎么用的。
echo_of.c 程序:
#include <stdio.h>
int main(void)
{
char ch;
while ((ch = getchar()) != EOF)
{
putchar(ch);
}
return 0;
}
编译得到 echo_eof.exe,再新建一个 words.txt:
在 words.txt 中写入以下内容:
按 Win+R,cd定位到程序目录。键入命令:echo_eof.exe < words.txt
可以看到,屏幕输出了 words.txt 中的内容。
输出重定向
输出重定向就是用文本文件代替屏幕当作程序的输出。’ > ’运算符是另一个重定向运算符。
echo_of.c 程序:
#include <stdio.h>
int main(void)
{
char ch;
while ((ch = getchar()) != EOF)
{
putchar(ch);
}
return 0;
}
编译得到 echo_eof.exe,再新建一个 words.txt:
按 Win+R,cd定位到程序目录。键入命令:echo_eof.exe > words.txt
输入内容,按 Ctrl+Z 结束输入。
在 words.txt 中成功看到输入的内容:
组合重定向
重定向符号可以组合。例如,你希望制作一份 words.txt 的副本,并命名为 mywords.txt。只需要输入以下命令:
echo_eof.exe > mywords.txt < words.txt
或 echo_eof.exe < words.txt > mywords.txt
。
两条命令的作用一致,因为命令与重定向符的顺序无关。
新建一个空的 mywords.txt:
按 Win+R,cd定位到程序目录。键入命令:echo_eof.exe > mywords.txt < words.txt
打开 mywords.txt,可以看到程序读入了 words.txt 的内容,并输出到 mywords.txt 中:
使用组合重定向要遵守的规则:
- 重定向运算符连接一个可执行程序(包括标准操作系统命令)和一个数据文件,不能连接2个程序或2个数据文件。
- 重定向运算符不能读取多个文件的输入,也不能把输出定向到多个数据文件。
- 文件名和运算符之间的空格不是必须的,除非偶尔要规避特殊字符。
8.5 小试牛刀:文件输出
不使用重定向,可以用程序直接打开文件。
file_eof.c 程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch;
FILE *fp;
char fname[50]; // 存储文件名
printf("Enter the file name: ");
scanf("%s", fname);
fp = fopen(fname, "r"); // 只读模式打开文件
if (fp == NULL)
{
printf("Failed to open file.\n");
exit(1); // 退出程序
}
// getc(fp)从打开的文件中获取一个字符
while ((ch = getc(fp)) != EOF)
{
putchar(ch);
}
fclose(fp); // 关闭文件
return 0;
}
按 Win+R,cd定位到程序目录。键入命令:file_eof.exe
,然后输入要打开的文件名。
可以看出,程序成功打印了 words.txt 的所有内容。
8.6 使用缓冲输入
guess.c 程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int guess = 1;
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 and with an n if it's wrong.\n");
printf("Uh...is your number %d?\n", guess);
while(getchar()!='y')
{
printf("Well, then, is it %d?\n",++guess);
}
printf("I knew I could do it!\n");
system("pause");
return 0;
}
运行结果:
可以看出,每次输入 n 时,程序打印了两条消息。这是因为输入 n 后,我们还按下了 Enter 进行换行,下一次循环中,getchar() 读取了这个换行符并打印了第二条消息。
修改后的 guess.c 程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int guess = 1;
char response;
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 and with an n if it's wrong.\n");
printf("Uh...is your number %d?\n", guess);
while ((response = getchar()) != 'y')
{
if (response == 'n')
printf("Well, then, is it %d?\n", ++guess);
else
printf("Sorry, I understand only y or n.\n");
while (getchar() != '\n')
continue;
}
printf("I knew I could do it!\n");
system("pause");
return 0;
}
运行结果:
8.7 混合数值和字符输入
getchar() 读取每个字符,包括空格、制表符、换行符。
而 scanf() 在读取数字时会跳过空格、制表符、换行符。
showchar.c 程序:
#include <stdio.h>
#include <stdlib.h>
void display(char c, int lines, int width);
int main(void)
{
char ch;
int rows, cols;
printf("Enter a character and two integers:\n");
while ((ch = getchar()) != '\n')
{
scanf("%d %d", &rows, &cols);
display(ch, rows, cols);
printf("Enter another character and two integers;\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");
system("pause");
return 0;
}
void display(char c, int lines, int width)
{
for (int row = 0; row < lines; row++)
{
for (int col = 0; col < width; col++)
{
putchar(c);
}
printf("\n");
}
}
运行结果:
可以看到,程序没等我们输入第二组数据就退出了。
我们知道,scanf() 在读取数字时会跳过空格、制表符、换行符,但换行符依旧在输入队列内,而 getchar() 读取每个字符,包括空格、制表符、换行符。因此,在进入下一轮迭代时,你还没来得及输入字符,getchar() 就读取了换行符,然后赋给了 ch,while 于是终止了循环。
要解决这个问题,程序要跳过第一轮输入结束和第二轮输入开始之间的所有换行符和空格。
修改后的 showchar.c 程序:
在这里插入代码片
运行结果:
8.8 输入验证
假设你编写了一个处理非负整数的循环,但是用户可能会输入一个负数。你可能会这样写:
long n;
scanf("%ld",&n);
while(n>=0)
{
// ...
scanf("%ld",&n); // 获取下一个值
}
但是,用户可能输入错误类型的值,比如字符。所以我们还需要检查 scanf() 的返回值。
上述程序修改为:
long n;
while(scanf("%ld",&n)==1 && n>=0)
{
// ...
scanf("%ld",&n); // 获取下一个值
}
checking.c 程序:
// checking.c -- 输入验证
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// 验证输入是一个整数
long get_long(void);
// 验证范围的上下限是否有效
bool bad_limits(long begin, long end, long low, long high);
// 计算a~b的整数平方和
double sum_squares(long a, long b);
int main(void)
{
const long MIN = -10000000L; // 范围的下限
const long MAX = +10000000L; // 范围的上限
long start; // 用户指定的范围最小值
long stop; // 用户指定的范围最大值
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"
"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 () for both "
"limits to quit):\n");
printf("lower limit: ");
start = get_long();
printf("upper limit: ");
stop = get_long();
}
printf("Done.\n");
system("pause");
return 0;
}
long get_long(void)
{
long input;
char ch;
while (scanf("%ld", &input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch); // 处理错误输入
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;
}
运行结果:
8.9 菜单浏览
menuette.c 程序:
/* menuette.c -- 菜单程序 */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
int choice;
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");
system("pause");
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("%d", &input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch); // 处理错误输出
printf(" is not an integer.\nPlease enter an");
printf("integer value,such as 15,-178,or 3:");
}
return input;
}
运行结果: