csci4061/lab12-code/QUESTIONS.txt
Michael Zhang 041f660ccd
f
2018-01-29 17:28:37 -06:00

123 lines
4.4 KiB
Text

__________________
LAB 12 QUESTIONS
__________________
- Name: (FILL THIS in)
- NetID: (THE kauf0095 IN kauf0095@umn.edu)
Answer the questions below according to the lab specification. Write
your answers directly in this text file and submit it to complete Lab01.
Problem 1 `text2bin.c'
======================
Examine the source code for `text2bin.c' which is meant to read a text
file such as `stocks-10.txt' and generate binary output to produce
binary files such as `stocks-10.dat'. Compile and run the program to
create files in the following way:
,----
| ./text2bin stocks-10.txt x.dat
`----
Describe the specific system call used to *write* binary
data. Describe whether binary files produced are human readable using
text tools like `cat' or a text editor.
The system call used is still write, but if you're using fwrite, you'd have to
use 'b' to make sure that certain characters that may coincide with line endings
(cough cough Windows) will not be transformed while reading the file. Binary
files are generally not human readable although if complete ASCII strings are
stored in the data file they will appear in the output of cat.
PROBLEM 2 `bin2text.c'
======================
Examine the source code for `bin2text.c' which is meant to read a
binary file such as `stocks-10.dat'. Compile this program using the
provided `Makefile' and run it on `stocks-10.dat' as in
,----
| ./bin2text stocks-10.dat
`----
Describe the specific system call used to read binary data and how it
is stored into variables/memory in the program.
The system call used to read is read. The stock2str function manipulates the
string and sscanfs all of the values out of it.
PROBLEM 3 `specific_stock.c'
============================
Examine the source code for `specific_stock.c' which is also meant to
read a binary file such as `stocks-10.dat' but takes a 2 inputs, a
file and number, and produces slightly different output. Compile this
program using the provided `Makefile' and run it on `stocks-10.dat'
trying different number arguments such as
,----
| ./specific_stock stocks-10.dat 0
| ./specific_stock stocks-10.dat 5
| ./specific_stock stocks-10.dat 17
`----
A
~
A naive technique to find a specific location in a file is to use a
loop as in.
,----
| for(int i=0; i<some_num; i++){
| read(..); // move ahead in file
| }
`----
When files are comprised of identically sized elements such as the
binary stock files, system calls can yield higher efficiency.
Describe the system call that is used in `specific_stock.c' to jump
immediately to a stock of interest. Describe the arguments of this
call and how they are calculated.
The system call used is lseek, and it's able to move the current offset of the
file descriptor. Because all of the stocks are the same size, it can just
calculate the number of bytes to skip and skip directly to the desired stock.
The arguments it takes are the file descriptor number, the offset bytes, and a
directive telling lseek how to use the second argument (SEEK_SET, SEEK_CUR, or
SEEK_END).
B
~
`specific_stock' checks that the stock requested is "in bounds" - not
beyond the end of the file implementing the following behavior:
,----
| $> ./specific_stock stocks-10.dat 17
| Off end of file
`----
Describe the technique and additional system call that is used to
accomplish this. Mention how the return value of `lseek()' is used.
It uses fstat, which gets the size of the file, and makes sure that the position
it's planning to jump to (which was obtained from the return value of lseek) is
less than the size of the file. Otherwise, it'll print "Off end of file" and
exit.
C
~
Examine the manual page for `lseek()'. Suggest how one might
accomplish the following tasks.
- Move the position of a file descriptor to the end of a file so that
subsequent writes append to the file
- Immediately read the last stock in a binary stock file
Both of these may be useful in a project.
(1) You could lseek(fd, 0, SEEK_END), which automatically seeks directly to the
end of the file (+0 bytes, which is the offset)
(2) ASSUMING the only data in this file is stock data, you could seek to the end
of the file minus the size of the stock struct. This would effectively seek to
the last stock in the file. lseek(fd, -sizeof(stock_t), SEEK_END)