Intro to C #1: select-column
Starter code: select-column-package.tar
Upload solutions via as: select-column.tar
Goals
- Get started with some simple manipulation of null-terminated char strings in C
- Get experience reading man pages
Rubric
CSV files
The input for the program you are about to write will come in the form of a comma-separated values file. CSV files give us a simple way of representing tables. Each line of text represents one row of the table, and each row is separated into columns by commas. For example, here is a simple CSV file animals.csv representing a table with a heading row followed by five data rows, with three data cells (or columns, if you prefer) per row:
The official CSV format has to worry about whether any of the data in the cells contain commas, but we're not going to worry about that. For this assignment, you may assume that there are no commas in the data, so any comma in the CSV file marks a separation between adjacent data cells.
Programming assignment #1: select-column
For this assignment, you will write a C program select-column.c
that takes a CSV file and a 1-based column number as command-line arguments, and
prints the specified column of the file to standard output.
For example, suppose we run your program like so:
where the file animals.csv is as shown above. Then the expected output is:
If the specified column does not exist in a particular row of the file, your program should print an empty line for that row.
What to do
- Extract select-column-package.tar into your working directory on mantis. See the How To page for a reminder of how this goes.
- Write your program in a file named
select-column.c. Debug, test, etc. until you're ready to hand it in. - Put your source file select-column.c in a folder named select-column.
- cd to the parent directory of select-column/
create a tar file:
tar cvf select-column.tar select-column- Download the tar file to your local machine (JupyterHub has right-click Download capability).
- Use Moodle to submit your tar file.
Note that I'm asking you to create a tar file instead of just submitting select-column.c
directly because I want you to practice creating tar files in preparation for later submissions
that may involve multiple files.
A little advice
- Think ahead of time about error handling. What could go wrong with the command-line or the input file or anything else in your program? What should your program do when those things happen?
- Think ahead of time about testing. What special cases should you test? How can you test them?
- Assume that each line ends either with a newline character
\n(ASCII 10) or with the end of the file. You'll want your program to work even if the last line doesn't end with\n. - For this program, you won't need to read entire lines into memory. This problem
is solvable just reading one character at a time using
fgetc.
Have fun!
Try stuff. Ask questions.