1. Goals for this week
-
Learn about command line arguments in C and the
atoi
function -
Learn about file I/O in C, using C’s stream interface for file I/O:
fopen
,fclose
,fscanf
-
Practice with an example visualization program that uses the ParaVis visualization library.
-
Some reminders about dynamically allocated 2-D arrays in C, and about using
gdb
andvalgrind
for debugging C programs -
Introduction to Lab 6.
2. Starting Point Code
Start by creating the week08
directory in
your inlab
subdirectory and copying over some files:
$ cd ~/cs31/inlab
$ mkdir week08
$ cd week08
$ pwd
/home/you/cs31/inlab/week08
$ cp ~richardw/public/cs31/week08/* ./
$ ls
3. C command line arguments and atoi
Let’s look at an example C program that takes command line arguments.
Start by opening commandlineargs.c
in an editor:
$ vim commandlineargs.c
The first thing to note is the change in `main’s definition:
int main(int argc, char *argv[]) { ...
The first parameter to main
, argc
, is the number of command line arguments.
For example, if the user enters:
./a.out 10 11 200
argc
will be 4 (a.out
counts as one of the command line arguments,
and 10, 11, and 200 as three more).
Each command line argument is passed to main
in the second parameter, argv
,
as a string (argv
is an array of strings):
-----
argv[0]:| *-|-----> "./a.out"
-----
argv[1]:| *-|-----> "10"
-----
argv[2]:| *-|-----> "11"
-----
argv[3]:| *-|-----> "200"
-----
argv[4]:| *-|-----| (NULL pointer: no more command line strings)
-----
C has functions that can convert strings of numeric characters to their int,
float, and other basic types, values. For example, atoi
converts a
string to an int:
int x = atoi(argv[1]); // x gets the int value 10
See the man page for atoi for more information
$ man atoi
Let’s try compiling and running this program with different command line arguments and see what happens.
4. File I/O in C
We are going to look at an example of file I/O in C, using C’s stream
interface for file I/O (fopen
, fclose
, fscanf
, fprintf
, etc.).
First lets look at some documentation about file I/O in C: File I/O in C
Next, open the fileio.c
program and let’s see what this code is doing.
Then try running it with the students.txt
file as a command line argument.
$ ./fileio students.txt
5. ParaVis Visualization Library
You will implement two different animation or visualization of the solution to your Lab 6 assignment. One of these will be a GUI (Graphical User Interface) visualization that uses the ParaVis library.
With the files you copied over is an example program that uses the ParaVis library. Let’s first try running it to see what it does:
$ ./visi_example 200 200 50
Then, let’s open up the source file and examine the code together:
$ vim visi_example.c
At the top of the file is a long comment about the steps a program
needs to take to initialize and use the ParaVis library to visualize
their program. Let’s first read through these, and then look at
this code together, starting with main
.
As we look at it, note how the struct
is being used, and the
argument to ParaVis library functions.
Let’s also look at the do_something
function that performs some
steps of updates to image buffer based on updates to application
program state. Note that after each call to the ParaVis draw_ready
function, the program calls usleep
to slow down the animation:
usleep(SLEEPYTIME); // otherwise animation too fast
Looking in the update_grid
function, you can see how the program
updates individual
RGB values of each element of the image buffer (2D array of color3
)
based on some function of program data and loop counter variables:
buff[buff_i].r = (data->grid[index]) % 256;
buff[buff_i].g = (data->grid[index] + j) % 256;
buff[buff_i].b = 200;
Try changing this code, change the color values and/or the function
used to set each color3
field value, recompile and see what happens.
Remember that each of the three fields of a color3
struct can contain
a value from 0 to 255.
There are a lot of website that will give you the RGB values for different colors. here is one example.
Documentation about using the ParaVisi interface is available as
a comment at the top of the visi_example.c
program. Also, more
detailed documentation is available in the pthreadGridVisi.h
header file:
$ vim /usr/local/include/qtvis/pthreadGridVisi.h
6. Reminder about gdb and valgrind for C program debugging
We are back to C programming this week, with pointers and functions.
You are all experts at using gdb to debug at the assembly level, but remember that you also know how to use gdb (and valgrind) to debug programs at the C code level.
Previously in lab, we looked at some examples of using both gdb and valgrind. Take a look at the In-Lab Assignment 5 page and the examples we talked about as a reminder of how to use these debugging tools.
7. Dynamically Allocated 2-D Arrays in C
We are not going to look at this program together in lab today, but
the twoDarray.c
program contains examples of C statically
declared 2D arrays and dynamically allocated 2D arrays (using the single
malloc
).
As you work on Lab 6, you can refer to this example program dynamically allocate and accesses a 2D array.
8. Handy References
-
Textbook. Chapter 2: I/O, 2D arrays, command line arguments in C. Chapter 3: C debugging tools (valgrind, gdb for C, gdb for assembly
-
Some C Programming Resources and Links including the C Style Guide, File I/O in C, cmdline args, and arrays