Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a good exercise for yourself to work on :)</p> <p>You should break your library into three parts</p> <ul> <li>Loading the CSV file</li> <li>Representing the file in memory so that you can modify it and read it</li> <li>Saving the CSV file back to disk</li> </ul> <p>So you are looking at writing a CSVDocument class that contains:</p> <ul> <li>Load(const char* file);</li> <li>Save(const char* file);</li> <li>GetBody</li> </ul> <p>So that you may use your library like this:</p> <pre><code>CSVDocument doc; doc.Load("file.csv"); CSVDocumentBody* body = doc.GetBody(); CSVDocumentRow* header = body-&gt;GetRow(0); for (int i = 0; i &lt; header-&gt;GetFieldCount(); i++) { CSVDocumentField* col = header-&gt;GetField(i); cout &lt;&lt; col-&gt;GetText() &lt;&lt; "\t"; } for (int i = 1; i &lt; body-&gt;GetRowCount(); i++) // i = 1 so we skip the header { CSVDocumentRow* row = body-&gt;GetRow(i); for (int p = 0; p &lt; row-&gt;GetFieldCount(); p++) { cout &lt;&lt; row-&gt;GetField(p)-&gt;GetText() &lt;&lt; "\t"; } cout &lt;&lt; "\n"; } body-&gt;GetRecord(10)-&gt;SetText("hello world"); CSVDocumentRow* lastRow = body-&gt;AddRow(); lastRow-&gt;AddField()-&gt;SetText("Hey there"); lastRow-&gt;AddField()-&gt;SetText("Hey there column 2"); doc-&gt;Save("file.csv"); </code></pre> <p>Which gives us the following interfaces:</p> <pre><code>class CSVDocument { public: void Load(const char* file); void Save(const char* file); CSVDocumentBody* GetBody(); }; class CSVDocumentBody { public: int GetRowCount(); CSVDocumentRow* GetRow(int index); CSVDocumentRow* AddRow(); }; class CSVDocumentRow { public: int GetFieldCount(); CSVDocumentField* GetField(int index); CSVDocumentField* AddField(int index); }; class CSVDocumentField { public: const char* GetText(); void GetText(const char* text); }; </code></pre> <p>Now you just have to fill in the blanks from here :)</p> <p>Believe me when I say this - investing your time into learning how to make libraries, especially those dealing with the loading, manipulation and saving of data, will not only remove your dependence on the existence of such libraries but will also make you an all-around better programmer.</p> <p>:)</p> <p><strong>EDIT</strong></p> <p>I don't know how much you already know about string manipulation and parsing; so if you get stuck I would be happy to help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload