gcht101hello/code20240727.html 20240727
Sorce Code:WECP100/book/cats
hello
    
// WECP100/book/cats.c 20240727
// description: cat simple using system call, on LINUX
// machine: laptop window11
// os: linux using wsl on windows11
//> wsl >> Welcome to Ubuntu 22.04.2 LTS //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: gcc --version >> gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
//cnano text.txt 00 0123456789 ... 06 0123456789
//$ gcc cats.c -o cats
//$ ./cats  < text.txt 
//  /00 0123456789
//  01 0123456789
//  02 0123456789
//  03 0123456789
//  04 0123456789
//  05 0123456789
// ./cats < text.txt  > out.txt
//  cmp  text.txt  out.txt
// ./cats < text1.txt 
// -bash: text1.txt: No such file or directory

#include< unistd.h>        // file read write
#include< stdio.h>         // perror() 
int main(void){
  ssize_t n;               // read bytes size
  char buffer[4096];       // read buffer
  while ((n = read(0, buffer, sizeof buffer)) > 0) {   //read from stdin:0
     if (write(1, buffer, n) < 0) {  //write to stdout:1 
       perror("write");     // error
       return 1;            // 
    }
  }
  // if (n < 0) {             // heading error read read
  //   perror("read");        // error message
  //   return 1;              // return error
  // }
}
//--------------------------------------------------