Lab 6: Input and Output
In this lab you'll extend your simple database to include "save" and "load" capabilities and also interact with a user.
Goals:
- Extend your database program to load and store its contents
- Interact with users through a small set of commands
- Create some automated tests for your database program
Part 1: Make dbe_print
more flexible
Right now you can only print to the screen, but today you'll be writing data to
a file as well. To make this happen, the first step is to change dbe_print
to print to a file of your choice.
- Add a
FILE*
parameter todbe_print
. - Change
dbe_print
to usefprintf
and print to that file. You'll have to modify bothdata.c
anddata.h
. For info on howfprintf
works, see the manpage (man 3 fprintf
). - Find everywhere that calls
dbe_print
and change it to work with your new parameter.
HINT: usegrep -n
to find locations in files that usedbe_print
.
HINT 2: In nano, you can use^-_
and enter 12 to go to line 12. In vi, hit escape and type12gg
to go to line 12.
HINT 3: You probably want yourdbe_print
calls to continue to print tostdout
.
Part 2: Implement getALine()
to read input
- Implement
getALine()
. The comments indata.c
should guide you. So will the manpage forfgets
. - Check if you're on the right track by making and running the
test
executable. - Once you've implemented
getALine()
, open uptest.c
and look at thetest_getALine(void)
function. It tests two cases. Add a third test.
Part 3: Adding an interaction prompt
Now that you can get input and write output, it's time to interact with a user. In this part of the lab, you'll be creating a run loop that repeatedly asks for input, processes the input (the command), then repeats.
To get started, open up local.c
and read the comment block above
handleLocalInput
. It explains all the commands you will implement!
The TODO
comments in the body of the function will tell you where to
implement things. Make sure the parts you implement work by running the
local
executable to test each new command you implement.
Some more information about the commands and suggestions below. We recommend completing the commands in this order:
The q
command
To quit, find a way to break out of the loop!
The l
command
When a user issues the l
, command, list all the database entries using dbe_print
.
(Local)> l x => y w => z (Local)>
The a
command
When a user issues the a
, command, prompt for two more inputs (one at a
time). A session should look like this:
(Local)> a name? myname value? myvalue (Local)>
In the last lab you implemented a helper function in data.c
that will do the
hard work. HINT: you can use the character buffers already declared inside the
function body.
The r
command
When a user issues the r
, command, prompt for one more input: the name to remove. Find the first match and remove that one. A session should look like this:
(Local)> r name? bob (Local)>
In the last lab you implemented a helper function in data.c
that will do the
hard work!
Part 4: Adding an export capability
The e
command
For this command, you need to implement the user-interaction parts in
local.c
, but also will need to implement some functionality in data.c
.
- See the comments in
data.c
andlocal.c
for details. - When you export the database, you're simply printing it into a file. Use
dbe_print
!
Part 5: Adding an import capability
The i
command
This is very similar to the e
command.
- See the comments in
data.c
andlocal.c
for details and hints.
Part 6: Going farther: Finding matches
This part is optional, though you'll need to complete it in the next lab if you don't do it here.
The f
command
Implement do_find_all_matches
, which is triggered by the 'f' command.
Hint: you use db_find_one
repeatedly until you run out of matches.
Finishing the Lab
- Make sure any code files you've created are added to SVN. (you can check
using
svn status
). - Ensure your name and your partner's names are on all the files you edited.
- Commit your files to SVN. Only one of you two must commit the code to SVN, but both your names must be on all files you changed.