CS201 Winter 2008 Assignment 4: File I/O and Command Line Arguments in C, Assignments of Computer Science

Information about assignment 4 for the cs201 course in winter 2008. The assignment involves modifying a simple c program to read from an optional command-line argument, creating and writing to a binary file, and reading from that file. Students are required to use the original while loop and not change any existing code. The document also includes grading criteria and deliverables.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-v4l
koofers-user-v4l 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS201 Winter 2008 Assignment 4 Due Feb 5, 2008
1. [10 points] B&O, problem 7.9
2. [10 points] Here is a simple program that copies its standard input to standard
output.
#include <unistd.h>
#define BUFFERSIZE 256
int main(int argc, char **argv)
{
int n;
char buf[BUFFERSIZE];
while((n = read(STDIN_FILENO,buf,
BUFFERSIZE)) > 0)
write(STDOUT_FILENO, buf, n);
}
Modify this program so that it takes an optional command line argument
infile. If infile is given, then copy infile to standard output; otherwise
do just what this program already does. The twist is that your program must use
the original while loop (the last two lines of code in main) for both cases. You are
only allowed to insert code, you are not allowed to change any existing code.
You can not write separate while loops for the two cases, and you can not change
the value of any constants such as STDIN_FILENO.
3. [15 points] B&O, 8.17
For # 3, write a small test bed program with a main() function that inputs a
command line from standard input, invokes mysystem(), and prints the exit status
of the child process. Use the test bed to test your function with various command
lines.
4. [15 points] This problem is to help you understand that files do not necessarily
contain ASCII characters. Write a C program called "readdouble" that will do the
following:
a. Initialize an array of 100 double precision floating point values such that
A[n] = n
b. Create a file and write the array to the file, not as character strings but as
an array of 8-byte objects, just as they appear in memory. You don't have
to know what these objects look like, you only have to write them to a file.
You can think of this file as containing a sequence of 100 records, each of
size 8 bytes and each containing a binary floating point value.
c. Seek to the thirty-fourth record in the file; that is, the one whose value is
34.0, and read that record into a double precision variable.
pf3
pf4

Partial preview of the text

Download CS201 Winter 2008 Assignment 4: File I/O and Command Line Arguments in C and more Assignments Computer Science in PDF only on Docsity!

  1. [10 points] B&O, problem 7.
  2. [10 points] Here is a simple program that copies its standard input to standard output.

#include <unistd.h> #define BUFFERSIZE 256 int main(int argc, char **argv) { int n; char buf[BUFFERSIZE];

while((n = read(STDIN_FILENO,buf, BUFFERSIZE)) > 0) write(STDOUT_FILENO, buf, n); }

Modify this program so that it takes an optional command line argument infile. If infile is given, then copy infile to standard output; otherwise do just what this program already does. The twist is that your program must use the original while loop (the last two lines of code in main ) for both cases. You are only allowed to insert code, you are not allowed to change any existing code. You can not write separate while loops for the two cases, and you can not change the value of any constants such as STDIN_FILENO.

  1. [15 points] B&O, 8.

For # 3, write a small test bed program with a main() function that inputs a command line from standard input, invokes mysystem() , and prints the exit status of the child process. Use the test bed to test your function with various command lines.

  1. [15 points] This problem is to help you understand that files do not necessarily contain ASCII characters. Write a C program called "readdouble" that will do the following:

a. Initialize an array of 100 double precision floating point values such that A[n] = n b. Create a file and write the array to the file, not as character strings but as an array of 8-byte objects, just as they appear in memory. You don't have to know what these objects look like, you only have to write them to a file. You can think of this file as containing a sequence of 100 records, each of size 8 bytes and each containing a binary floating point value. c. Seek to the thirty-fourth record in the file; that is, the one whose value is 34.0, and read that record into a double precision variable.

d. Print the value of that variable as a string to the standard output, using printf. e. The program must take one command line argument, specifying the name of the file to create. f. Do not use the Rio package. You may use Unix system calls or the stdio package to read and write the file.

Deliverables

Please read the Deliverables information on the homework page.

Grading Criteria

The assignment is worth a total of 50 points. Each item below indicates attributes the assignment should have, and points are deducted from 50 accordingly if those attributes are lacking.

Assignment submitted on time (50 points)

Assignment turned in correctly (up to 5 points each bullet)

  • Emailed to the correct email address
  • Code and answers to questions are in a tar file attached to the email.
  • There is no more than one attachment. The grader has only to save the attachment and extract the tar file to get all the necessary files.
  • Code is in one or more source files, and answers to questions are in a separate text file.
  • The tar file contains a directory with the name of the student (login name or first or last name, something recognizable), and files under that directory
  • All files are plain text; no binaries or word processor documents
  • A Makefile makes the programs
  • Code, when extracted from the tar file, builds and runs on Linux

Answer to problem #

  • Answers to problems are correct (up to 10 points)
  • Answers are written in a way that is clear and understandable (up to 5 points)

Correctness of code, problem 2

  • Code works correctly as specified above. (up to 10 points)
  • Uses the common "while" loop, whether or not an argument was given (up to 10 points)
  • Detects when it is not able to open the input file and terminates with a message (up to 5 points)
  • Comments indicate what is not obvious from the code itself. Each function should be headed by a brief comment saying what it does. The comment sho uld not tell the data types of arguments or return value, because the function declaration itself does that. The comment should not tell where the function is called from, because that might change. The comment should tell any side effects the function has, other than its return value.
  • It is not necessary to comment the majority of lines of code; only things that really could use some explanation.