CS 243 Homework #3
10/23/06
In your home directory,
create a subdirectory named: homework3
Part 1:
In your homework3 directory,
create part1.c, compile, and run part1
which should do the following:
writes these 3 letters: abc
followed by newline
to part1.out
in the current directory.
Create the file if it does not already exist.
Over write the file if it already exists.
Hint: see open on p. 98, write on p. 96.
Testing:
put more than 10 chars in part1.out
then run your program
make sure there are only 4 chars after your program runs.
Part 2:
In your homework3 directory,
create part2.c, compile, and run part2
which should do the following:
writes these 3 letters: abc
followed by newline
to part2.out
in the current directory.
Create the file if it does not already exist.
Set the permissions so that everyone can read, write, and
execute if umask is 0.
Append to the file if it already exists.
Hint: set umask to 0 before you test your program.
You program should not call chmod.
Part 3:
Initially part3.c should be a copy of part2.c.
but change all references from part2 to part3.
Then, if your program is unable to create or write to the
destination file,
use perror (see p 126)
to show the error message.
Hint: turn off write perm to the file part3.out
before you run your program.
Another way to test: turn off write perm
to this dir so that you can't create any files in it.
Make sure that your program shows an error msg
if you can't create the file
and if you can't write to an existing file.
Hint 2:
Check for errors twice:
1. immediately after open (error here should be fatal)
2. immediately after write
Your code to check for errors should look like this:
errno=0;
/* make some system call */
if (errno) {
perror("progname or function");
exit(errno); /* if the error is fatal */
}
It is wrong to only check errno after the write,
because,
if the open fails,
the file descriptor returned by open will not be valid
so the write will get an error
which will set errno
but this error will be misleading
and won't show you the open error.