程序清单
8.1
/* echo.c -- 重复输入 */
// 该程序获取从键盘输入的字符,并把这些字符发送到屏幕上。
//输入: Hello, there. I would
#include <stdio.h>
int main()
{
char ch;
while (( ch = getchar() ) != '#')
putchar (ch);
return 0;
}
8.2
/* echo_epf.c -- 重复输入,直到文件结尾 */
#include <stdio.h>
int main()
{
int ch;
while((ch = getchar()) != EOF)
putchar(ch);
return 0;
}
8.3
/* file_eof.c -- 打开一个文件并显示该文件 */
#include <stdio.h>
#include <stdlib.h> //为了使用exit()
int main()
{
int ch;
FILE * fp;
char fname[50]; //储存文件名
printf("Enter the name of the file: ");
scanf("%s", fname);
fp = fopen (fname, "r"); // 打开带读取文件
if ( fp == NULL ) // 如果打开失败
{
printf("Failed to open the file: ");
exit (1); // 退出程序
}
// get(fp) 从打开的文件获取一个字符
while ((ch = getc(fp)) != EOF)
putchar(ch);
fclose(fp); // 关闭程序
return 0;
}
8.4
/* guess.c -- 一个拖沓且错误的猜数字程序 */
#include <stdio.h>
int main()
{
int guess = 1;
char response;
printf("Pick an integer from 1 to 100. I will try to your guess it.\n");
printf("Respond with a