zoukankan      html  css  js  c++  java
  • Linuxc

    输入流stdin默认是键盘,输出流stdout默认是显示器,错误流stderr

    #include <stdio.h>
    
    int main()
    {
        printf("请输入选择的数字:
    "); // 标准输出流
        int choice;
        scanf("%d",&choice); // 标准输入流
        printf("您输入的数字是:%d
    ",choice);
    }
    
    root@jiqing:~/cspace/les4# ./cio.out
    请输入选择的数字:
    10
    您输入的数字是:10
    
    
    #include <stdio.h>
    
    int main()
    {
        // printf("please input the value a: 
    ");
        fprintf(stdout,"please input the value a: 
    "); // 非标准输出流
    
        int a;
    
        // scanf("%d",&a);
        fscanf(stdin,"%d",&a); // 非标准输入流
        if (a<0) {
            fprintf(stderr,"the value must > 0 
    ");
            return 1;
        }
        return 0;
    }
    
    
    root@jiqing:~/cspace/les4# ./cio.out 
    please input the value a: 
    -1
    the value must > 0 
    
    

    重定向

    #include <stdio.h>
    int main()
    {
        printf("please input value of i:
    ");
        int i;
        scanf("%d",&i);
    
        printf("please input value of j:
    ");
        int j;
        scanf("%d",&j);
    
        printf("i+j=%d
    ",i+j);
        return 0;
    }
    
    
    root@jiqing:~/cspace/les5# cc main.c -o main.out && ./main.out
    please input value of i:
    10
    please input value of j:
    20
    i+j=30
    
    

    管道重定向处理

    root@jiqing:~/cspace/les5# ./main.out  1>> a.txt
    10
    20
    root@jiqing:~/cspace/les5# cat a.txt
    please input value of i:
    please input value of j:
    i+j=30
    
    

    这个时候会将所有的标准输出流都写入到a.txt中。

    root@jiqing:~/cspace/les5# ./main.out  1> a.txt
    10
    20
    root@jiqing:~/cspace/les5# cat a.txt
    please input value of i:
    please input value of j:
    i+j=30
    
    

    单箭头不会累计数据,每次都是最新的数据。

    重定向输入流。

    新建一个input.txt

    10
    30
    
    root@jiqing:~/cspace/les5# ./main.out < input.txt
    please input value of i:
    please input value of j:
    i+j=40
    
    

    直接就输出了结果,键盘都没有敲。

    标准错误流,

    #include <stdio.h>
    int main()
    {
        printf("please input value of i:
    ");
        int i;
        scanf("%d",&i);
    
        printf("please input value of j:
    ");
        int j;
        scanf("%d",&j);
        if (0!=j) {
            printf("%d/%d=%d
    ",i,j,i/j);
        } else {
            fprintf(stderr,"j must > 0
    ");
            return 1;
        }
        return 0;
    }
    
    
    root@jiqing:~/cspace/les5# ./main.out 1>t.txt 2>f.txt
    10
    0
    root@jiqing:~/cspace/les5# cat t.txt
    please input value of i:
    please input value of j:
    root@jiqing:~/cspace/les5# cat f.txt
    j must > 0
    
    

    错误流会重定向到f.txt中,正确流会到t.txt中。

    三者结合使用,

    root@jiqing:~/cspace/les5# vim input.txt
    
    10
    0
    
    root@jiqing:~/cspace/les5# ./main.out 1>t.txt 2>f.txt <input.txt
    root@jiqing:~/cspace/les5# cat t.txt
    please input value of i:
    please input value of j:
    root@jiqing:~/cspace/les5# cat f.txt
    j must > 0
    
  • 相关阅读:
    centos7上安装memcached以及PHP安装memcached扩展(二)
    centos7上安装memcached以及PHP安装memcached扩展(一)
    centos7上安装redis以及PHP安装redis扩展(二)
    centos7上安装redis以及PHP安装redis扩展(一)
    Redis Desktop Manager无法连接虚拟机中启动的redis服务问题排查步骤
    CentOS 7设置开机启动服务,添加自定义系统服务
    delphi 选择文件夹目录保存
    mysql drop database ERROR 2013
    delphi 在别的方法 调用keypress事件
    delphi Inc Dec用法
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/8336374.html
Copyright © 2011-2022 走看看