CSV Parsing
Algorithm
Site Map Feedback

Download:

Up BlockFile Collections CSV

Read Comma Separated Value (CSV) Files

This class was designed to read Comma Separated Value (.CSV) files but has been extended to read other similar formats as well.
CSV Files are text files using one line to represent one Record (Record Separators are \r\n characters).
Fields are separated by a particular character (comma , by default).
eg:
one,2,three,four,5.0

If a Field contains a Field Separator character it should be enclosed in Text Separators (Speech Marks " by default).
eg:
one,2,three,"four, or 4",5.0

If a Field contains a Text Separator, that Text Separator should be doubled up, and the Field should be enclosed in Text Separators.
eg:
the fourth field of:
one,2,three,"4'5"" tall",5.0
would be read as:
4'5" tall

Leading and trailing white space characters are removed.

You can specify multiple field separators:
CString S;
CCSV CSV(&S, "1234,432.234;346:787 65", ",;: ");
ASSERT(CSV.GetFieldCount()==5);
The class also handles e-mail recipient lists
where a field may contain a section in Text Separators followed by a section which will never contain Text or Field separators.
eg:
"Smith, John" <JS@Work.com>, "Jones David"   <DJ@Work.com>, Me@Home.com
would be read as the following three fields:
"Smith, John" <JS@Work.com>
"Jones David" <DJ@Work.com>
Me@Home.com
Note that the white-space characters between the two sections are replaced with a single space character.

Examples:
#include "CSV.h"

CString S;
CCSV MT(&S,"");
ASSERT(MT.GetFieldCount()==0);
ASSERT(!MT.GetNextField());
ASSERT(S=="");
ASSERT(MT.GetFieldAt(0)=="");
ASSERT(MT.FindField("")==-1);
ASSERT(MT.FindField("none")==-1);

CCSV CSV(&S," one,\"two\",\"thr,ee\" ,\"fo\"\"ur\", \"Smith, John\" <JS@Work.com>");
ASSERT(CSV.GetFieldCount()==5);
ASSERT(CSV.FindField("none")==-1);
ASSERT(CSV.GetNextField());
ASSERT(S=="one");
ASSERT(CSV.GetFieldAt(0)=="one");
ASSERT(CSV[0]=="one");
ASSERT(CSV.FindField("one")==0);
ASSERT(CSV.GetFieldCount()==5);
ASSERT(CSV.GetNextField());
ASSERT(S=="two");
ASSERT(CSV.GetFieldAt(1)=="two");
ASSERT(CSV[1]=="two");
ASSERT(CSV.FindField("two")==1);
ASSERT(CSV.GetFieldCount()==5);
ASSERT(CSV.GetNextField());
ASSERT(S=="thr'ee");
ASSERT(CSV.GetFieldAt(2)=="thr'ee");
ASSERT(CSV.FindField("thr'ee")==2);
ASSERT(CSV.GetFieldCount()==5);
ASSERT(CSV.GetNextField());
ASSERT(S=="fo\"ur");
ASSERT(CSV.GetFieldAt(3)=="fo\"ur");
ASSERT(CSV[3]=="fo\"ur");
ASSERT(CSV.FindField("fo\"ur")==3);
ASSERT(CSV.GetFieldCount()==5);
ASSERT(CSV.GetNextField());
ASSERT(S=="\"Smith, John\" <JS@Work.com>");
ASSERT(CSV.GetFieldAt(4)=="\"Smith, John\" <JS@Work.com>");
ASSERT(CSV[4]=="\"Smith, John\" <JS@Work.com>");
ASSERT(CSV.FindField("\"Smith, John\" <JS@Work.com>")==4);
ASSERT(CSV.GetFieldCount()==5);
ASSERT(!CSV.GetNextField());
ASSERT(S=="");
ASSERT(CSV.GetFieldAt(5)=="");
ASSERT(CSV[5]=="");
ASSERT(CSV.GetFieldCount()==5);
To read from a file:
  CFile File;
  if(!File.Open("Test.csv", CFile::modeRead|CFile::shareDenyNone)) return;
  CArchive Ar(&File, CArchive::load);
  CString S;
  while(Ar.ReadString(S)) { // For all lines of the file
    CCSV CSV;
    CString Field;
    CSV.Set(&Field,S);
    while(CSV.GetNextField()) { // For all Fields of the Record
      // The current Field is now stored in CString Field
  } }
To read command line arguments from GetCommandLine()
(you should really use __argc and __argv[]):
  CString S;
  CString First ;
  CString Second;
  CString Third ;
  CCSV CSV(&S, GetCommandLine(), ' ');
  CSV.GetNextField(); // Application Path
  if(!CSV.GetNextField()) return true; // No Parameters
  CString argv(S);
  if(CSV.GetNextField() // First  Parameter
  && CSV.GetNextField() // Second Parameter
  && CSV.GetNextField() // Third  Parameter
  && !CSV.GetNextField()) {
    ...
  }
To create a folder from a Path:
  CString CreateBranch(const CString& Path) { // Returns the path that was successfully created.
    CCSV CSV(0, Path, '\\');
    if(Path.Left(2)=="\\\\") { // \\ComputerName\Share\Directory format
      CSV.GetNextField(); // Skip the first two "empty fields"
      CSV.GetNextField();
      CSV.GetNextField(); // Win9x needs the ComputerName and Share to be used together, so skip the ComputerName too.
    }
    while(CSV.GetNextField()
       && (SetCurrentDirectory(CSV.GetDone())
        ||     CreateDirectory(CSV.GetDone(),0)));
  return CSV.GetDone(); // Could be return CSV.GetEnd().IsEmpty() if you wanted the function to return a bool...

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.