Lab Assignment--C++ File I/O and Parsing Line Input


General Goal:

Create a C++ class and test program that:

Details:

Create the implementation (oneline.cpp) and test program (main.cpp) for the following oneline.h file:

#include <string>
using namespace std;

class OneLine
{
   public:
      OneLine();
      ~OneLine();
      void breakLine();
      void printReverse();
      istream &readLine(istream& is);
      string returnLine();
   private:
      string oneLine;
      char **words;
      int wordCount;
      void resetLine();
};

The following provides a description of each of the functions implemented in oneline.cpp:

OneLine
  • initialize oneLine to empty string
  • initialize wordCount to 0
breakLine
  • create a temporary C String version of oneLine
  • use strtok to break the temporary line into words
  • allocate enough space to hold all of the words and store them in words
  • store a count of the number of words in wordCount
  • (this is meant as a general algorithm, you may have to fiddle a little to get this working. Hint: this may involve cycling through all the words with strtok twice)
printReverse
  • cycle through the words from wordCount-1 to 0
  • print each word as you are cycling
readLine
  • call resetLine to free up memory and reset oneLine and wordCount to empty string and zero respectively
  • read one line from is (in this case, a file stream) and store it in oneLine
  • return is
returnLine
  • return oneLine
resetLine
  • if allocation has occurred than free up the words
  • set oneLine to be an empty string
  • set wordCount to zero
~OneLine
  • if allocation has occurred than free up the words

The following provides the algorithm of main.cpp:

  1. create an ifstream for the file test.txt (please use this file for testing)
  2. create a OneLine object
  3. while you can still read from the file:
    1. call readLine
    2. call breakLine
    3. call printReverse
  4. close the file

For this assignment please use strdup whenever you need to allocate space and copy a string.

Output:

The following is a link to the output produced using test.txt:

Deliverables: