c primer plus第8章总结:字符输入输出

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

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

3、混合输入:
     scanf() 会在遇到第一个空格、制表符,换行符时,会停止读取;
     getchar() 会读取输入的每一个字符;
      
# 只允许输入整数
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类型输入,排除字母类输入:
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;
}

# 只允许输入一个字符
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;
}



课后习题
# 第1题 统计从文件输入到文件结尾的字符数;
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");
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值