Linux stream redirection is a powerful feature where program inputs and outputs can be hooked together seamlessly by the system to build compound commands. In this studio, you will:
read()
and write()
functions to
read input and write output
Please complete the required exercises below, as well as any optional enrichment exercises that you wish to complete.
As you work through these exercises, please record your answers in a text file. When finished, submit your work by sending your text file and source code to the git repository under the appropriate folder.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
cat
. This program concatenates (appends) any
provided input to standard output. It's a useful way to print text
files to the terminal, or to write bits of data into a text file.
Go ahead and type the command "cat
" and hit enter. Then,
write a few lines of text to the console (and hit enter after each line).
As the answer to this exercise, describe what happens.
You can type CTRL-D on a blank line in order to quit cat
.
Typing CTRL-D sends a special "end of file" character to the terminal.
cat
is to print text files to
the console. Find your favorite text file if you'd like, or you can download
one of mine with the wget
command:
wget http://cs.slu.edu/~dferry/courses/csci3500/studios/mars.txt --no-check-certificate
In the last exercise, the cat
program got its data from
standard input (i.e. the keyboard). One neat feature of Linux is that it allows
us to redirect streams like standard input and standard output. Using
your text file, redirect standard input with the '<
'
character as such:
cat < mars.txt
As the answer to this exercise, describe what happens.
>
' character into a new text file. Use the syntax:
cat > new.txt
Enter a few lines of text. Open up your text file to see the results.
Remember that you can quit cat
with CTRL-D.
As the answer to this exercise, describe what happens.
>>
'
instead of '>
'). This appends your input to the end
of the file, rather than overwriting the start of the file.
Leave this answer blank.
cat
. Start by creating a new file called copy.c
and fill it out with the framework for an empty program (see Studio 01 if
you'd like to see that framework again).
Look at the manual pages for the system calls read()
and
write()
, and look at your code from Studio 01 to see how you
used write()
. What header file do you need to include to use
these system calls?
#define bufferSize 200
Then allocate an array of characters to be your buffer:
char buffer[ bufferSize ];
Leave this answer blank.
cat
is to read input forever until it
encounters an end of file character. Look up the documentation for the
read()
system call. What is the return value type
for read()
? What is the specific return value that indicates
the end of a file?
STDIN_FILENO
) into the buffer.
break;
" out of the loop.
STDOUT_FILENO
). Be
careful to use the return value from read()
so as not to write more than was read.
When you are satisfied with your code, compile it and test it in all the ways
we tested cat
above. Does your program's behavior match? Note that
you will need to invoke your program with "./
", as in:
./copy < mars.txt
bufferSize
will affect
the correctness of your program? What if bufferSize
is very
small, or even equal to one? Try a few different values for
bufferSize
and record the results.
bufferSize
versus having a large one?
read()
and write()
, try using the
libc functions fgets()
and fprintf()
.