In this activity, we learn how to use iterator (e.g., hashtable_iterate(), counters_iterate()) to go through a hashtable and save its content to an external file.

Grab the skeleton code iterator:

$ cp -r /thayerfs/courses/22spring/cosc050/workspace/iterator .
$ cd iterator/

save.c is the skeleton code where we created a hashtable and inserted dummy content. Our task is to fill in the to-do to write the hashtable content to an external file.

To compile the code, first generate the libcs50.a library under libcs50/, then use the library to generate the executable:

$ make -C libcs50/
make: Entering directory '/thayerfs/home/f001cxb/cs50/demos/iterator/libcs50'
cp libcs50-given.a libcs50.a
make: Leaving directory '/thayerfs/home/f001cxb/cs50/demos/iterator/libcs50'

Once you are done, the program should run like this (here I save the content to a file named test):

$ ./save test
$ cat test 
wonder woman: 3 6 4 8

Work in groups to finish the program. Use gdb and valgrind for debugging if necessary.

Solution

save-solution.c