title: C++: Parsing and Reading Text File Into an Array
body: <p>I was hoping that I could find some help here. I have an assignment due next week that involves reading a bunch of data from a txt file into an array, and then printing out the results. The data is in the following format:</p>
  
  <p>"Macbeth","William Shakespeare","41.04","161","23","978-88-5985-004-5"</p>
  
  <p>"A Christmas Carol","Charles Dickens","98.74","167","547","978-26-2885-780-7".
  .</p>
  
  <p>.</p>
  
  <p>.</p>
  
  <p>Each row has six pieces of information that I need to store for later use. I'm supposed write code that counts the number of lines of text we have in order to create a dynamic array of the correct size. I've got that covered. I've got 39 lines entries. Then I'm supposed to create a function that reads the txt file and saves all of that data to the corresponding object in the array that I created.</p>
  
  <p>I don't know what methods to use, and I've been looking around for tutorials and explanations for a few days. I have extremely limited experience with files and parsing, so excuse me if I'm a little inexperienced. Here's my code so far:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iomanip&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  
  class Author
  {
      public:
  
  private:
      string fname, lname;
  
  };
  
  class Book
  {
      friend ofstream&amp; operator&lt;&lt;(ofstream&amp;, Book);
  
          public:
          Book();
  
      private:
          string bookName;
          Author author;
          double price;
          int qtyOnHand;
          int qtySold;
          double revenue;
          string ISBN;
  
  };
  
  Book :: Book()
  {
  
  }
  
  int getLineNumber(ifstream &amp;);
  void parseData(ifstream &amp;, Book []);
  
  
  //void sortBookList(Book[], int, int);
  
  
  int main()
  {
      int numberOfBooks;
  
      //open the file from which to read the data
      ifstream myFile;
      myFile.open("Book List.txt");
      //function to find out how many objects to create
      numberOfBooks = getLineNumber(myFile);
  
      //create an array with that many objects
      Book *bptr;
      bptr = new Book[numberOfBooks];
      //function to read information from file into array of objects
      parseData(myFile, bptr);
  
      //loop to call sorting function and output function based on 4 different criteria
  
      //close the file explicitly
      return 0;
  }
  
  int getLineNumber(ifstream &amp;myFile)
  {
      int counter = 0;
      string myString;
  
  
      while(!myFile.eof())
      {
          getline(myFile, myString);
          counter++;
      }
  
      myFile.close();
  
      counter --;
      return counter;
  }
  
  void parseData(ifstream &amp;myFile, Book bookPtr[])
  {
  
  }
  </code></pre>
  
  <p>So, to summarize my issues, I don't understand how to parse data from the text file into my array.
  A very big thank you in advance to anyone that could help! Cheers.</p>
  
  <p>EDIT: I've tried fooling around with the code, and I think I made a step in the right direction, but I'm still a little lost. Here's what I have for the parseData function.</p>
  
  <pre><code>void parseData(ifstream &amp;myFile, Book bookPtr[])
  {
  
      string dummyLine;
  
      string word, line;
      myFile.open("Book List.txt");
      getline(myFile, dummyLine);
      string data[6];
  
      while(!myFile.eof())
      {
          getline(myFile, line, '\n');
  
          for (size_t i = 0; i &lt; line.size(); ++i)
          {
              char c = line[i];
  
              if(c == ',' || c == '\n')
              {
                  if(!word.empty())
                  {
                      data[i] = word;
                      word.clear();
                  }
              }
              else
              {
                  word += c;
              }
  
  
          }
          if(!word.empty())
          {
              //cout &lt;&lt; word &lt;&lt; endl;
          }
      }
  
  
  
  
  }
  </code></pre>