Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If what you're really doing is manipulating a CSV file itself, Nelson's answer makes sense. However, my suspicion is that the CSV is simply an artifact of the problem you're solving. In C++, that probably means you have something like this as your data model:</p> <pre><code>struct Customer { int id; std::string first_name; std::string last_name; struct { std::string street; std::string unit; } address; char state[2]; int zip; }; </code></pre> <p>Thus, when you're working with a collection of data, it makes sense to have <code>std::vector&lt;Customer&gt;</code> or <code>std::set&lt;Customer&gt;</code>.</p> <p>With that in mind, think of your CSV handling as two operations:</p> <pre><code>// if you wanted to go nuts, you could use a forward iterator concept for both of these class CSVReader { public: CSVReader(const std::string &amp;inputFile); bool hasNextLine(); void readNextLine(std::vector&lt;std::string&gt; &amp;fields); private: /* secrets */ }; class CSVWriter { public: CSVWriter(const std::string &amp;outputFile); void writeNextLine(const std::vector&lt;std::string&gt; &amp;fields); private: /* more secrets */ }; void readCustomers(CSVReader &amp;reader, std::vector&lt;Customer&gt; &amp;customers); void writeCustomers(CSVWriter &amp;writer, const std::vector&lt;Customer&gt; &amp;customers); </code></pre> <p>Read and write a single row at a time, rather than keeping a complete in-memory representation of the file itself. There are a few obvious benefits:</p> <ol> <li>Your data is represented in a form that makes sense for your problem (customers), rather than the current solution (CSV files).</li> <li>You can trivially add adapters for other data formats, such as bulk SQL import/export, Excel/OO spreadsheet files, or even an HTML <code>&lt;table&gt;</code> rendering.</li> <li>Your memory footprint is likely to be smaller (depends on relative <code>sizeof(Customer)</code> vs. the number of bytes in a single row).</li> <li><code>CSVReader</code> and <code>CSVWriter</code> can be reused as the basis for an in-memory model (such as Nelson's) without loss of performance or functionality. The converse is not true.</li> </ol>
 

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