Simulate the Linux command tee in C

后端 未结 3 1704
我在风中等你
我在风中等你 2021-01-21 13:03

I have to do the simulation of the command tee in C for Linux. How does tee work internally? It looks like a T-shaped pipe, so should I use a pipe? Is

3条回答
  •  無奈伤痛
    2021-01-21 13:52

    I finished the program!

    #include 
    #include 
    #include 
    
    main(int argc, char *argv[]){
    
    FILE *fp, *fp1;
    char buffer;
    
    if(argc != 4){
        printf("\nError");
        printf("\nSintaxis: tee [archivo1] [archivo2]\n");
        exit(0);
    }
    
    if(strcmp(argv[1], "tee") == 0){
        fp = fopen(argv[2], "r");
        fp1 = fopen(argv[3], "w");
    
        printf("\Content in %s:\n", argv[2]);
    
        while(!feof(fp)){
            buffer = fgetc(fp);
            fputc(buffer, fp1);
            printf("%c", buffer);
        }
    
        printf("\n\n%s received %s\n", argv[3], argv[2]);   
    
        fclose(fp);
        fclose(fp1);
        }
        else
            printf("\nThe first argument have to be tee\n");
    }
    

提交回复
热议问题