zoukankan      html  css  js  c++  java
  • 程序运行

    1 理解test.c, 说出程序功能
    2 编译运行程序,提交运行截图
    #include    <stdio.h>
    #include    <stdlib.h>
    #include    <fcntl.h>
    #include <unistd.h>
    int main()
    {
        int    fd ;
        int    newfd;
        char    line[100];
    
        fgets( line, 100, stdin ); printf("%s", line );//读入一个字符串,并将字符串打印出来
        fgets( line, 100, stdin ); printf("%s", line );
        fgets( line, 100, stdin ); printf("%s", line );
    
        fd = open("data", O_RDONLY);//    open(filename, O_RDONLY)打开一个想连接到stdin上的文件data
    
        newfd = dup2(fd,0);    // newfd 与fd 关联的文件关联起来 ;让本来指向标准输出缓冲区的描述符0指向文件data
        
        if ( newfd != 0 ){
            fprintf(stderr,"Could not duplicate fd to 0
    ");
            exit(1);//直接退出
        }
        close(fd);//关闭fd,这时候任何想从标准输入读取数据的函数都将从次文件中读入。
    
        fgets( line, 100, stdin ); printf("%s", line );
        fgets( line, 100, stdin ); printf("%s", line );
        fgets( line, 100, stdin ); printf("%s", line );
        return 0;
    }

    1、首先采集三次用户输入并打印出来

    2、以只读的方式打开data文件,记为fd

    3、用newfd关联fd关联的文件并打印用户输入,失败,输出“不能复制fd”

    下面两个:

    ①没有data文件

    ②有data文件:文件内容为hello,20181217!

     

  • 相关阅读:
    bzoj1081 [SCOI2005]超级格雷码
    bzoj3790 神奇项链
    bzoj2822 [AHOI2012]树屋阶梯
    bzoj1485 [HNOI2009]有趣的数列
    bzoj1486 [HNOI2009]最小圈
    bzoj2721 [Violet 5]樱花
    POJ 1238 Substrings
    ZOJ Team Formation
    POJ 1459 Power Network
    POJ 1458 Common Subsequence
  • 原文地址:https://www.cnblogs.com/cindy123456/p/14036777.html
Copyright © 2011-2022 走看看