__________________ 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 ./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)