Week 07 Tutorial Questions
-
How is the assignment going?
Does anyone have hints or advice for other students?
Has anyone discovered interesting cases that have to be handled?
-
How do I reuse functions from
print_instruction
forexecute_instruction
? -
You work on the assignment for an hour tonight. What do you need to do when you are finished?
-
For which MIPS instructions does
execute_instruction
need to call set_byte more than once? -
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. -
Why should you not use fgets(3) or fputs(3) with binary data?
-
What does the following printf(3) statement display?
printf ("%c%c%c%c%c%c", 72, 101, 0x6c, 108, 111, 0x0a);
Try to work it out without simply compiling and running the code. The ascii(7) manual page will help with this; read it by running
. Then, check your answer by compiling and running.man 7 ascii
-
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?
-
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);
-
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. -
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?