Computer Science 381
Programming Unix in C
Winter Immersion 2016, The College of Saint Rose
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.
%s
, which means to expect an additional parameter which is
a character string (well, really a pointer to a
null-terminated array of char). Here, the string is
argv[0], the first command-line parameter, which is always
the name of the program. This labels the error message with the
program name.
%s
's, so we have
two additional parameters to fprintf, both pointers to
strings.
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
%d
in the format
string), and put it into the place pointed at by the address of
x, then return the number of values that matched the input
with the correct format." Similarly for the double value
using a %lf
in the format string.
&
operator.
Don't worry, it will make better sense when you see more examples.
&
operator. This is because the name of a
C string already is a pointer to the first element in the array.
Again, much more on this when we study C pointers in more detail.
File I/O
Read Section 7.5 of K&R to learn about reading from and writing to files in C.
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:
\n
in your fscanf format string,
you can automatically move the input to the next line.
char str1[256]; char str2[256];
the following would copy the contents of str1 to str2:
strcpy(str2, str1);
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 |