您的位置:首页 > 其它

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]
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 scanf