gcht101hello/code20240721.html 20240721
Sorce Code:WECP100/catc03.c
hello

    // WECP100/catc03.c 20240721
    // description: cat command, read stdin if no argument given.
    // using system call onLINUX 
    // machine: laptop window11
    // os: linux using wsl on windows11
    // > wsl on power shell
    // cat /etc/os-release >> PRETTY_NAME="Ubuntu 22.04.2 LTS"
    // uname -a >> inux LAPTOP-2E5CQCBT 6.6.36.3-microsoft-standard-WSL2 #1
    //          >> Sat Jun 29 07:01:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
    // language: g++ --version, g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 
    //$ g++ catc03.cpp -o catc03
    //$ ./catc03 \n  123 until ^C >> 123
    //  ./catc03 text.txt  >>  1234567890
    //  ./catc03 text.txt text.txt >> 1234567890 \12345n 67890
    #include   // for standard input/output (like fgetc, putchar).
    #include  // for exit function.
    void doCat(FILE *f);// Reads characters from pointer,Prints each character 
    int main(int argc, char *argv[]){
        if (argc == 1) {   // no argument given.
            doCat(stdin);  // read standard input 
        }else{
            for (int i = 1; i < argc; i++) {
                FILE *f;
                f = fopen(argv[i], "r");  //file open  read mode
                if (!f) {             // Error Handling
                    perror(argv[i]);  // error message 
                    exit(1);          // exit
                }
                doCat(f);   // read file 
                fclose(f);  // file close
            }
        }
        exit(0);
    }
    //-------------------------------------
    void doCat(FILE *f){
        int c;
        while ((c = fgetc(f)) != EOF) {
            if (putchar(c) < 0) exit(1);
        }
    }