出處:http://stackoverflow.com/questions/12398947/simulate-the-linux-command-tee-in-c
add comment (requires an account with 50 reputation) |
tee takes stdin and copies the data stream to stdout as well as a file given as an option, it can be used in many very different situations. An implementation in C is quite simple, just make a program that copies all data from stdin to stdout, but also use the same output statements for stdout on a file that you opened based on the command line argument. basically in pseudo code: file f = open(argv[1]) while (! end of file stdin) { buffer = read stdin write stdout buffer write f buffer } close(f) Note that you don’t really have to do anything with pipes, your shell will sort out the pipes, the program only has to copy data from one stream to two others. | |||||
add comment (requires an account with 50 reputation) |
I finished the program! Thank you so much 🙂 #include <stdio.h> #include <string.h> #include <stdlib.h> 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("nn%s received %sn", argv[3], argv[2]); fclose(fp); fclose(fp1); } else printf("nThe first argument have to be teen"); } | |||
add comment (requires an account with 50 reputation) |