Standard i o
This presentation is the property of its rightful owner.
Sponsored Links
1 / 8

Standard I/O PowerPoint PPT Presentation


  • 43 Views
  • Uploaded on
  • Presentation posted in: General

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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

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()


Shell redirection

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?


Redirect standard input i

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;

}


Redirect standard input ii

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;

}


Redirect standard output

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;

}


File access

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


Pipes

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


  • Login