Standard streams (ITI8510)

Allikas: Lambda
Real-Time Operating Systems and Systems Programming
[ @ ]

This exercise is about standard streams. While we will return to the specific inputs, it is vital to understand how the streams work for most of the exercises.

Task

  1. Write a program named show_input which reads file 'input.txt' ( fopen(), fread(), fclose() ) and displays it to the standard output (use fwrite() or fprintf instead of printf() and use stdout stream for output).
  2. Write a program named my_cat which reads from stdin and then outputs each line to stdout using only fgetc() and fputc() functions.
Find a way to use the program to:
  • Show a text file
  • Copy a file
  • Create and edit a text file
  1. Fix your first program so that it shows error messages to stderr using either fprintf() or perror()

Hints

  • These tasks are simple enough to figure out without getting off-the-shelf answer from google. Your brain will thank you. If you still want to copy example code -- do it by hand at least. It's pointless waste of your own time to copy-paste and fix code you found online.
  • You have variables named stdin, stdout and stderr which correspond to streams which the system opens for you upon program launch. They arrive with inclusion of stdio.h.
  • Standard streams can be redirected on the command-line using < > 2> and | signs. You should know them, so find out what they do. A useful keyword is "linux pipes".
  • If the task seems too simple: try the Nonblocking IO version of the my_cat utility.