69 lines
5.6 KiB
Plaintext
69 lines
5.6 KiB
Plaintext
{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\froman Times New Roman;}{\f3\froman\fprq2 Times New Roman;}}
|
||
{\colortbl\red0\green0\blue0;\red0\green0\blue255;\red255\green0\blue0;}
|
||
\deflang1033\horzdoc{\*\fchars }{\*\lchars }\pard\plain\f3\fs48\cf1\b COXCsvFile\plain\f3\fs48\b
|
||
\par \plain\f3\fs16\b Copyright \'a9 Dundas Software Ltd. 1997 - 1999, All Rights Reserved
|
||
\par \plain\f3\fs24
|
||
\par \plain\f3\fs24\b COXCsvFile\plain\f3\fs24 extends the MFC class \plain\f3\fs24\b CStdioFile\plain\f3\fs24 by adding functions for processing the lines of a CStdioFile as rows in a comma delimited data file (\plain\f3\fs24\b CSV\plain\f3\fs24 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.
|
||
\par
|
||
\par 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.
|
||
\par
|
||
\par The reading/writing information from/to CSV file can be outlined in the following steps.
|
||
\par
|
||
\par
|
||
\par \plain\f3\fs24\b Reading from CSV file.
|
||
\par \plain\f3\fs24
|
||
\par 1)\~\~\~ Instantiate \plain\f3\fs24\b COXCsvFile\plain\f3\fs24 by specifying path to the \plain\f3\fs24\b CSV file\plain\f3\fs24 or pointer to already \plain\f3\fs24\b opened FILE object\plain\f3\fs24 as a parameter in the object's \plain\f3\fs24\cf1\ul constructor
|
||
\par \plain\f3\fs24
|
||
\par 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:
|
||
\par
|
||
\par \pard\li360\ri360\plain\f3\fs24 BOOL \plain\f3\fs24\cf1\ul GetColumns\plain\f3\fs24 ();
|
||
\par
|
||
\par \pard\plain\f3\fs24 After columns are initialized you might retrieve the number of columns found in the CSV using:
|
||
\par
|
||
\par \pard\li360\ri360\plain\f3\fs24 int \plain\f3\fs24\cf1\ul GetColumnCount\plain\f3\fs24 () const;
|
||
\par
|
||
\par \pard\plain\f3\fs24 If the CSV file doesn't contain the name of columns then you have to specify them explicitely using:
|
||
\par
|
||
\par \pard\li360\ri360\plain\f3\fs24 void \plain\f3\fs24\cf1\ul SetColumns\plain\f3\fs24 ();
|
||
\par
|
||
\par \pard\plain\f3\fs24 3)\~\~\~ After column info has been gathered/set we can read the contents of the CSV file \plain\f3\fs24\b row by row\plain\f3\fs24 using the following function:
|
||
\par
|
||
\par \pard\li360\ri360\plain\f3\fs24 BOOL \plain\f3\fs24\cf1\ul ReadLine\plain\f3\fs24 ();
|
||
\par
|
||
\par \pard\plain\f3\fs24 If this function succeed then we can retrieve data in particular column using set of \plain\f3\fs24\cf1\ul ReadColumn\plain\f3\fs24 () functions.
|
||
\par
|
||
\par Note, that \plain\f3\fs24\b COXCsvFile\plain\f3\fs24 class allows you to read CSV files with \plain\f3\fs24\b multiple tables\plain\f3\fs24 . Refer to the \plain\f3\fs24\b CsvTest sample\plain\f3\fs24 in the \plain\f3\fs24\cf2 .\\samples\\database\\CsvTest\plain\f3\fs24 subdirectory for an example of reading CSV files with multiple tables.
|
||
\par
|
||
\par \~
|
||
\par \plain\f3\fs24\b Writing to CSV file.
|
||
\par \plain\f3\fs24
|
||
\par 1)\~\~\~ Instantiate \plain\f3\fs24\b COXCsvFile\plain\f3\fs24 by specifying \plain\f3\fs24\b path to the CSV file\plain\f3\fs24 or pointer to already \plain\f3\fs24\b opened FILE object\plain\f3\fs24 as a parameter in the object's \plain\f3\fs24\cf1\ul constructor
|
||
\par \plain\f3\fs24
|
||
\par 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:
|
||
\par
|
||
\par \pard\li360\ri360\plain\f3\fs24 void \plain\f3\fs24\cf1\ul SetColumns\plain\f3\fs24 ();
|
||
\par
|
||
\par \pard\plain\f3\fs24 Then you must call the following function in order to physically write the headers in to the file:
|
||
\par
|
||
\par \pard\li360\ri360\plain\f3\fs24 void \plain\f3\fs24\cf1\ul WriteHeaders\plain\f3\fs24 ();
|
||
\par
|
||
\par \pard\plain\f3\fs24 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:
|
||
\par
|
||
\par \pard\li360\ri360\plain\f3\fs24 void \plain\f3\fs24\cf1\ul Initialize\plain\f3\fs24 ();
|
||
\par
|
||
\par \pard\plain\f3\fs24
|
||
\par 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\~ \plain\f3\fs24\cf1\ul WriteColumn\plain\f3\fs24 () functions.
|
||
\par
|
||
\par When you set data to every column you can call \plain\f3\fs24\cf1\ul WriteLine\plain\f3\fs24 () function in order to save the current row to the CSV file.
|
||
\par
|
||
\par Note, that \plain\f3\fs24\b COXCsvFile\plain\f3\fs24 class allows you to write CSV files with \plain\f3\fs24\b multiple tables\plain\f3\fs24 . Refer to the \plain\f3\fs24\b CsvTest sample\plain\f3\fs24 in the \plain\f3\fs24\cf2 .\\samples\\database\\CsvTest\plain\f3\fs24 subdirectory for an example of writing CSV files with multiple tables.
|
||
\par
|
||
\par
|
||
\par \plain\f3\fs24\b Dependency:
|
||
\par \pard\li360\ri360\plain\f3\fs24\cf2 "OXCsvFile.h"\plain\f3\fs24
|
||
\par \pard\plain\f3\fs24 \~
|
||
\par \plain\f3\fs24\b Source code files:
|
||
\par \pard\li360\ri360\plain\f3\fs24\cf2 "OXCsvFile.cpp"\plain\f3\fs24
|
||
\par \pard\plain\f2\fs20
|
||
\par }
|
||
|