Week 08 Tutorial Questions
Objectives
- introduce the basic concepts of the unix filesystem, files & directory tree structure
- introduce methods of accessing and manipulating files in C
Code Review
The code review should be on mathematics.s. The reviewees should give a brief description of their code, and the class should ask questions, comment on the quality of the code, and suggest improvements. Each review should take about 10 minutes.Code reviews should be conducted in the second hour of tutorials in the week.
Tutorial Questions
-
What does fopen(3) do? What are its parameters?
-
What are some circumstances when fopen(3) returns NULL?
-
How do you print the specific reason that caused fopen(3) to return
NULL
? -
Write a C program,
first_line.c
, which is given one command-line argument, the name of a file, and which prints the first line of that file tostdout
. If given an incorrect number of arguments, or if there was an error opening the file, it should print a suitable error message. -
Write a C program,
write_line.c
, which is given one command-line argument, the name of a file, and which reads a line fromstdin
, and writes it to the specified file; if the file exists, it should be overwritten. -
Write a C program,
append_line.c
, which is given one command-line argument, the name of a file, and which reads a line fromstdin
and appends it to the specified file. -
Write a program
file_max.c
that reads a set of integer numbers from a file and prints out the maximum number to standard output. The name of the input file should be specified as a command line argument. -
Why should you not use fgets(3) or fputs(3) with binary data?
-
How many different values can fgetc(3) return?
-
Why are the names of fgetc(3), fputc(3), getc(3), putc(3), putchar(3), and getchar(3) misleading?
-
Write a C program,
print_borts_file.c
, which prints the contents of a file containing borts.A bort is an unsigned two-byte big-endian integer (bort is a contraction of big-endian short).
The possible bort values are 0..65535.
For example:
./print_borts_file test.borts.txt bort 0: 34 bort 1: 35 bort 2: 36 bort 3: 37 bort 4: 38 bort 5: 39 bort 6: 40 bort 7: 41 bort 8: 42
Side note: the linux utilitiesod
andxxd
are good ways to inspect binary files such as bort files. -
We say that the Unix filesystem is tree-structured, with the directory called
/
as the root of the tree, e.g.,Answer the following based on the above diagram:
What is the full pathname of COMP1521's
web
directory?Which directory is
~jas/../..
?Links to the children of a given directory are stored as entries in the directory structure. Where is the link to the parent directory stored?
What kind of filesystem object is
cat
?What kind of filesystem object is
home
?What kind of filesystem object is
tty0
?What kind of filesystem object is a symbolic link? What value does it contain?
Symbolic links change the filesystem from a tree structure to a graph structure. How do they do this?
-
For each of the following calls to the
fopen()
library function, give anopen()
system call that has equivalent semantics relative to the state of the file.fopen(FilePath, "r")
fopen(FilePath, "a")
fopen(FilePath, "w")
fopen(FilePath, "r+")
fopen(FilePath, "w+")
Obviously,
fopen()
returns aFILE*
, andopen()
returns an integer file descriptor. Ignore this for the purposes of the question; focus on the state of the open file. -
Consider the
lseek(fd, offset, whence)
function.What is its purpose?
When would it be useful?
What does its return value represent?
-
Consider a file of size 10000 bytes, open for reading on file descriptor
fd
, initially positioned at the start of the file (offset 0). What will be the file position after each of these calls tolseek()
? Assume that they are executed in sequence, and one will change the file state that the next one deals with.lseek(fd, 0, SEEK_END);
lseek(fd, -1000, SEEK_CUR);
lseek(fd, 0, SEEK_SET);
lseek(fd, -100, SEEK_SET);
lseek(fd, 1000, SEEK_SET);
lseek(fd, 1000, SEEK_CUR);
-
If a file
xyz
contains 2500 bytes, and it is scanned using the following code:int fd; // open file descriptor int nb; // # bytes read int ns = 0; // # spaces char buf[BUFSIZ]; // input buffer fd = open ("xyz", O_RDONLY); assert (fd >= 0); while ((nb = read (fd, buf, 1000)) > 0) { for (int i = 0; i < nb; i++) if (isspace (buf[i])) ns++; } close (fd);
Assume that all of the relevant
#include
's are done.How many calls with be made to the
read()
function, and what is the value ofnb
after each call?
Revision questions
The following questions are primarily intended for revision, either this week or later in session.
Your tutor may still choose to cover some of these questions, time permitting.
-
Write a C program, fgrep.c, which is given 1+ command-line arguments which is a string to search for.
If there is only 1 command-line argument it should read lines from stdin and print them to stdout iff they contain the string specified as the first command line argument.
If there are 2 or more command line arguments, it should treat arguments after the first as filenames and print any lines they contain which contain the string specified as the first command line arguments.
When printing lines your program should prefix them with a line number.
It should print suitable error messages if given an incorrect number of arguments or if there is an error opening a file.
-
Write a C program,
print_file_bits.c
, which given as a command line arguments the name of a file contain 32-bit hexadecimal numbers, one per line, prints the low (least significant) bytes of each number as a signed decimal number (-128..127).