Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Standard I/O FILE * stdin: standard input (read-only) FILE * stdout: standard output (write-only) FILE * stderr: standard error output (write- only)

Similar presentations


Presentation on theme: "1 Standard I/O FILE * stdin: standard input (read-only) FILE * stdout: standard output (write-only) FILE * stderr: standard error output (write- only)"— Presentation transcript:

1 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 2 Standard I/O Example #include 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 3 Shell Redirection Replace stdin/stdout/stderr by another file Usage:./a.out < input.txt./a.out > output.txt./a.out output.txt Question: how to achieve this?

4 4 Redirect Standard Input (I) #include 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 5 Redirect Standard Input (II) #include 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 6 Redirect Standard Output #include 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 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 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


Download ppt "1 Standard I/O FILE * stdin: standard input (read-only) FILE * stdout: standard output (write-only) FILE * stderr: standard error output (write- only)"

Similar presentations


Ads by Google