COXCsvFile
Copyright © Dundas Software Ltd. 1997 - 1999, All Rights Reserved

COXCsvFile extends the MFC class CStdioFile by adding functions for processing the lines of a CStdioFile as rows in a comma delimited data file (CSV file). Each column, or field, of a row can be accessed independently. When reading the data from the fields, the field name is used to determine which field on a row to access. This allows a programmer to process the fields irregardless of their order within the comma delimited file. 

One rule for these files is that the fields have to be named. Preferably the names of the fields will be given by the first row in the file when it is being read in or alternately a programmer can assign them at run-time.

The reading/writing information from/to CSV file can be outlined in the following steps.

Reading from CSV file.

1)    Instantiate COXCsvFile by specifying path to the CSV file or pointer to already opened FILE object as a parameter in the object's constructor

2)    If the CSV file contains the name of columns as one of its line (usually it is the first line unless you are trying to read CSV file with multiple tables) then initialize internal array of columns name using the following function:

BOOL GetColumns();

After columns are initialized you might retrieve the number of columns found in the CSV using:

int GetColumnCount() const;

If the CSV file doesn't contain the name of columns then you have to specify them explicitely using:

void SetColumns();

3)    After column info has been gathered/set we can read the contents of the CSV file row by row using the following function:

BOOL ReadLine();

If this function succeed then we can retrieve data in particular column using set of ReadColumn() functions.

Note, that COXCsvFile class allows you to read CSV files with multiple tables. Refer to the CsvTest sample in the .\samples\database\CsvTest subdirectory for an example of reading CSV files with multiple tables.

Writing to CSV file.

1)    Instantiate COXCsvFile by specifying path to the CSV file or pointer to already opened FILE object as a parameter in the object's constructor

2)    If you want the resulting CSV file to contain the name of columns as one of its line (usually it is the first line unless you are trying to write CSV file with multiple tables) then initialize internal array of columns name using the following function:

void SetColumns();

Then you must call the following function in order to physically write the headers in to the file:

void WriteHeaders();

In the case you don't want to include headers you have to call the following function that will prepare the CSV file for writing data:

void Initialize();

3)    After the column info has been specified we can populate the CSV file with data. You can set data to particular column in the current row using set of  WriteColumn() functions.

When you set data to every column you can call WriteLine() function in order to save the current row to the CSV file.

Note, that COXCsvFile class allows you to write CSV files with multiple tables. Refer to the CsvTest sample in the .\samples\database\CsvTest subdirectory for an example of writing CSV files with multiple tables.

Dependency:
"OXCsvFile.h"
 
Source code files:
"OXCsvFile.cpp"