1 / 8

Standard I/O

Standard I/O. FILE * stdin: standard input (read-only) FILE * stdout: standard output (write-only) FILE * stderr: standard error output (write-only) File descriptor: stdin: 0 stdout: 1 Stderr: 2. Standard I/O Example. #include <stdio.h> int main() { char buf[1024];

Download Presentation

Standard I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Standard I/O • FILE * stdin: standard input (read-only) • FILE * stdout: standard output (write-only) • FILE * stderr: standard error output (write-only) • File descriptor: • stdin: 0 • stdout: 1 • Stderr: 2

  2. Standard I/O Example #include <stdio.h> int main() { char buf[1024]; int offset, read_num=0, write_num=0; read_num = read(0, buf, sizeof(buf)); while(read_num > 0){ offset = 0; do { write_num = write(1, buf+offset, read_num); //write_num = fwrite(buf, 1, read_num, stdout); read_num -= write_num; offset += write_num; } while(read_num > 0); read_num = read(0, buf, sizeof(buf)); } return 0; } • read(): return # of bytes read, 0 indicates end of file • write(): return # of bytes write • fwrite(): calls write()

  3. Shell Redirection • Replace stdin/stdout/stderr by another file • Usage: • ./a.out < input.txt • ./a.out > output.txt • ./a.out < input.txt > output.txt • Question: how to achieve this?

  4. Redirect Standard Input (I) #include <stdio.h> int main() { char buf[1024]; int offset, read_num=0, write_num=0; close(0); open(“input.txt", O_RDONLY); read_num = read(0, buf, sizeof(buf)); while(read_num > 0){ offset = 0; do { write_num = write(1, buf+offset, read_num); read_num -= write_num; offset += write_num; } while(read_num > 0); read_num = read(0, buf, sizeof(buf)); } return 0; }

  5. Redirect Standard Input (II) #include <stdio.h> int main() { char buf[1024]; int offset, read_num=0, write_num=0; int input_fd = open("tmp.txt", O_RDONLY); dup2(input_fd, 0); read_num = read(0, buf, sizeof(buf)); while(read_num > 0){ offset = 0; do { write_num = write(1, buf+offset, read_num); read_num -= write_num; offset += write_num; } while(read_num > 0); read_num = read(0, buf, sizeof(buf)); } return 0; }

  6. Redirect Standard Output #include <stdio.h> int main() { char buf[1024]; int offset, read_num=0, write_num=0; int input_fd = open("tmp.txt", O_RDONLY); dup2(input_fd, 0); read_num = read(0, buf, sizeof(buf)); close(1); open("temp.txt", O_WRONLY | O_TRUNC | O_CREAT,0666); while(read_num > 0){ offset = 0; do { write_num = write(1, buf+offset, read_num); read_num -= write_num; offset += write_num; } while(read_num > 0); read_num = read(0, buf, sizeof(buf)); } return 0; }

  7. File Access • Three access levels: • User, Group, Other • For each level, read/write/execute represented by a bit • Example: ls -l shellp.C -rw-r----- 1 bing cse_fac 12187 Sep 20 13:39 shellp.C

  8. Pipes • Unidirectional flow: • Write on source end • Read from sink end • Example: • System call: int pipe(int * filedes) • Hints: • use multiple-processes, wait() cat words.txt | sort | uniq | cat > uniq-sorted-words.txt

More Related