Computer Science 381
Programming Unix in C

Winter Immersion 2016, The College of Saint Rose

Lab 3: Input/Output in C
Due: Monday, December 28, 2015

In this lab, you will work through some examples focused on how C handles input and output, including to and from files.

Before you start, create a directory on a Unix or Mac system to contain your work for this lab. You may do your work on any C-capable Unix-like system (such as a lab Mac or mogul).

Readings from K&R

Skim through Chapters 2 and 3 of K&R. Most of what is there is identical to or at least very similar to what you likely know well from other programming languages you have used. Much of what we will look at this lab is in Chapter 7, though that chapter assumes you have seen some of the material in Chapters 4-6. Even so, it is useful to be able to use higher-level input and output right away, so we will consider much of the content of that chapter in this lab as well.

Command-line Parameters

You have likely seen Java applications that take command-line parameters (the String args[] parameter to the main method of a class). A C program that wishes to make use of command-line parameters must declare two parameters to the main function, traditionally named argc and argv.

The parameter argc to the main function is a count of how many command-line strings are included in argv, which is an array of strings.

See Example:
/home/cs381/examples/printargs

Note: argv[0] is not the first parameter, it is the program name itself, and this array entry for the program name is included in the value of argc.

Even when we enter numbers for command-line parameters, the operating system will provide them to your program as strings. So we need to be able to convert strings to a numeric equivalent.

See Example:
/home/cs381/examples/repeat

This is done with the overly complicated strtol function, which we use, then check error conditions. There's a lot here we have not yet seen.

Practice Program: Write a C program that takes an arbitrary number of command-line parameters, each of which should represent an integer value. Print out the sum of the values provided. Call your C program argadder.c (5 points)

Formatted Keyboard Input

We have seen how to use the getchar function to get input from the keyboard or redirected from a file, one character at a time. But often, we'd like to read input as words or numbers.

C's standard mechanism for this is the scanf function.

See Example:
/home/cs381/examples/scanf-example

Practice Program: Write a program inputadder.c that takes its input from the keyboard rather than from the command line. Your program should read in integer values one by one, accumulating a sum as you go, until you encounter an invalid (non-integer) or the end of the input (someone types Ctrl-d). At that point, print out the sum and exit. (5 points)

File I/O

Read Section 7.5 of K&R to learn about reading from and writing to files in C.

Practice Program: Write a program everynth.c that takes 3 command-line parameters: the names of two files and a number we'll call n. The program should take read a series of integers from the first file, and write every nth number to the second file. (10 points)

Programming Assignment Preview

Note: the programming assignment portion will be due as part of the next lab submission, but you are strongly encouraged to get as much of it done this week as possible.

There is collection of graph data files that represent various highway systems on this page. Each of the ".gra" files is in the format described under "Graph Data" here.

Write a program extremes.c that reads the "waypoints" portion of a graph file whose file name is specified as a command-line parameter and find the easternmost, westernmost, northernmost, and southernmost waypoints in the file. For each, print out the waypoint label and its latitude and longitude.

Notes:

This program will be worth 30 points, broken down as shown at the end of our next lab assignment.

Executables for reference solutions to all programs are available on mogul in /home/cs381/labs/io.

Submission

Please submit all required files as email attachments to terescoj AT strose.edu by Monday, December 28, 2015. Be sure to check that you have used the correct file names and that your submission matches all of the submission guidelines listed on the course home page. In order to email your files, you will need to transfer them from mogul to the computer from which you wish to send the email. There are a number of options, including the sftp command from the Mac command line.

Grading

This lab is graded out of 20 points.

Grading Breakdown

Practice program argadder.c correctness 5 points
Practice program inputadder.c correctness 5 points
Practice program everynth.c correctness 10 points
Total 20