CSE110 Spring 2001

Homework #9 Hints


  1. Since you are no longer assigning string constants to the name and description fields, you will need to allocate storage for the strings you read from the file. One easy way to do this is to change the types of the fields of your ObjectDescription struct to be arrays of some fixed size. You can #define a constant to set this maximum size. Otherwise, if you use type char * (which is the real type of string, remember), you will need to use malloc to allocate storage for each field before you try to store anything there.

    Since reading each entry from the input file requires the same code regardless of what's actually in the entry, it is natural to write a function to take care of reading one entry. This function should take a file pointer (which is assumed to be already open for reading) and a pointer to a single ObjectDescription as arguments, and doesn't need to return anything. It should then get an entire line and copy it the the name field for the ObjectDescription stucture it got as an argument, read the next line and copy it to the description field, and read the third (empty) line and do nothing with it. Note that just assigning the (character array) buffer you read from the input to the structure field won't work because arrays cannot be copied by assignment (think about using strcpy). Note also that if the method you use to read in each line also reads in a newline, you will need to do something about removing the newline from the line (by finding it and changing it to a null character, or maybe think about using strncpy instead of strcpy, in order to copy only the front part of the line, without the newline).

    You might also want to take a look at the post in the newsgroup on the feof function to help you construct your file input code.

    You will also need something to assign to your object type field (the one named obj_type in the solutions) for the objects described in the input file other than the first five. If you used an enumerated type for this field, you could just add one more member like OBJ_EXTRA and assign this same value for all of the extra objects. They need not have a unique value in this field since none of them will be treated specially - Ringo can only look at them, pick them up, examine them, and throw them.

  2. Note that reading the file works exactly the same as in problem 1, so if you implemented the function described above, then half of your work is done for you - just copy into your new program!

    To simplify the structure of your main program, it would be a good idea to implement functions printDescriptions that prints all valid entries to the terminal, as you must do on every command, readDescriptions which reads from the input file and initializes your ObjectDescription array, and writeDescriptions, a function that writes the valid entries back out to the file.

    The remainder of the program just requires writing a control block much like the one you wrote long ago for the simulator, which reads commands from the user and executes them, and then prompts for another command. Note, though, in this case, you will need to use a cascaded-if statement instead of a switch statement since you can't switch on strings.