|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article presents the Why CDataFile?When I set out to search for a class to read CSV and other text data, I had these criteria:
To my surprise, I was hard pressed to find anything that met my criteria. I
found some stuff that used OBDC and even DOM, but nothing lightweight, simple to
use, or very fast. So I decided to write my own, and Using CDataFileIn order to use #include "DataFile.h" I have listed the member functions and operators of CDataFile Member FunctionsCDataFile Operators
Constructing a CDataFileCDataFile(void); CDataFile(const int& dwReadFlags); CDataFile(const char* szFilename, const int& dwReadFlags = DF::RF_READ_AS_DOUBLE); CDataFile(const DF::DF_SELECTION& df_selection); CDataFile(const CDataFile& df); Parameters
RemarksNone of the Example// Construct an empty CDataFile: CDataFile df; // Construct a CDataFile to read a CSV file containing data of type double: CDataFile df("C:\\MyData.csv"); // Construct a CDataFile to read a CSV file containing data of type string: CDataFile df("C:\\MyStringData.csv", DF::RF_READ_AS_STRING); // Construct an empty CDataFile that will read string data and append // data with each subsequent file read: CDataFile df(DF::RF_READ_AS_STRING | DF::RF_APPEND_DATA); // Construct a CDataFile from a subset of another CDataFile: CDataFile df1("C:\\MyData.csv"); CDataFile df2(df1(iLeftColumn, iTopRow, iRightColumn, iBottomRow)); CDataFile::AppendData()The bool AppendData(const int& iVariable, const char* szValue); bool AppendData(const int& iVariable, const double& value); bool AppendData(const int& iVariable, const std::vector<double>& vData); bool AppendData(const int& iVariable, const std::vector<std::string>& vszData); Parameters
Return ValueReturns RemarksIf the user appends Example// To append 0.2123 to the variable at index 0: if(!df.AppendData(0, 0.2123)) cout << df.GetLastError(); // To append the contents of a std::vector<double>, v, to the variable at index 4: if(!df.AppendData(4,v); cout << df.GetLastError(); CDataFile::ClearData()The void ClearData(void); RemarksThe class will reclaim a block of contiguous memory equal to the size specified by
Example// Flush the contents of a CDataFile, df. df.ClearData(); CDataFile::CreateVariable()The member function, bool CreateVariable(const char* szName, const double& initial_value, const int& iSize = 0); bool CreateVariable(const char* szName, const std::string& initial_value, const int& iSize = 0); bool CreateVariable(const char* szName, const std::vector<double>& vData); bool CreateVariable(const char* szName, const std::vector<std::string>& vszData); Parameters
Return ValueReturns RemarksThe default size of a new variable is 0. The user must then call Example// Create a variable in df named "MyVar" // with 27 samples initialized to 0.0. if(!df.CreateVariable("MyVar", 0.0, 27)) cout << df.GetLastError(); // Create a variable in df named "My Vector Var" // containing the data in vector, v. if(!df.CreateVariable("My Vector Var",v); cout << df.GetLastError(); CDataFile::DeleteVariable()The bool DeleteVariable(const int& iVariable); Parameters
Return ValueReturns RemarksUse
Example// Delete the variable at index 3 from CDataFile, df. if(!df.DeleteVariable(3)) cout << df.GetLastError(); CDataFile::FromVector()The static CDataFile FromVector(const char* szVariableName, const std::vector<double>& vData); static CDataFile FromVector(const char* szVariableName, const std::vector<std::string>& vData); Parameters
Return ValueReturns a
RemarksUse this static function when you need to convert a vector of data
to a
Example// Add the contents of vector, v, to CDataFile, df, using operator += df += CDataFile::FromVector("MyVectorData", v); // Combine the contents of vector, v1, // and vector, v2, and set the CDataFile, df, // equal to the result. df = CDataFile::FromVector("My V1 Data", v1) + CDataFile::FromVector("My V2 Data", v2); CDataFile::GetData()There are several overrides provided for the member function,
double GetData(const int& iVariable, const int& iSample); double GetData(const char* szVariableName, const int& iSample); int GetData(const int& iVariable, std::vector<double>& rVector); int GetData(const char* szVariableName, std::vector<double>& rVector); int GetData(const int& iVariable, const int& iSample, char* lpStr); int GetData(const char* szVariableName, const int& iSample, char* lpStr); int GetData(const int& iVariable, const int& iSample, std::string& rStr); int GetData(const char* szVariableName, const int& iSample, std::string& rStr); int GetData(const int& iVariable, std::vector<std::string>& rVector); int GetData(const char* szVariableName, std::vector<std::string>& rVector); Parameters
Return Valuedouble GetData(const int& iVariable, const int& iSample); double GetData(const char* szVariableName, const int& iSample); RemarksIf calling Example// Initialize a variable, d, to sample 7 of "MyVar" from CDataFile, df. double d = df.GetData("MyVar", 7); if(d == DF::ERRORVALUE) cout << df.GetLastError(); // Get string data from sample 3 of variable 9 from CDataFile, df. CString szValue = ""; int iLength = 0; iLength = df.GetData(9, 3, szValue.GetBuffer(0)); szValue.ReleaseBuffer(); if(iLength == -1) cout << df.GetLastError(); else { //... do something } // Get all the data from "My Variable 2" out of CDataFile, // df, and put it in vector, vData. std::vector<double> vData; int iSize = df.GetData("My Variable 2", vData); if(iSize == -1) cout << df.GetLastError(); else { //... do something } CDataFile::GetLastError()The member function, const char* GetLastError(void) const; Return ValueReturns a
Example// Display the last error encountered by CDataFile, df. cout << df.GetLastError(); CDataFile::GetNumberOfSamples()The int GetNumberOfSamples(const int& iVariable) const; Parameters
Return ValueReturns the number of samples if the function was successful, -1 if an error was encountered. RemarksUse
Example// Get the number of samples contained // in the variable at index 3 from CDataFile, df. int nSamps = df.GetNumberOfSamples(3); if(nSamps==-1) cout << df.GetLastError(); CDataFile::GetNumberOfVariables()The int GetNumberOfVariables(void) const; Return ValueReturns the number of variables contained in the
Example// Get the number of variables contained in CDataFile, df. int nVars = df.GetNumberOfVariables(); CDataFile::GetReadFlags()The int GetReadFlags(void) const; Return ValueReturns the current read flags. RemarksThe function returns an Example// Check to see if the user has set DF::RF_APPEND_DATA cout << "Append mode is " << (df.GetReadFlags() & DF::RF_APPEND_DATA ? "" : "NOT " ) << "set!"; CDataFile::GetVariableIndex()The int GetVariableIndex(const char* szVariableName, const int& iStartingIndex = 0); int GetVariableIndex(const char* szVariableName, const char* szSourceFilename, const int& iStartingIndex = 0); Parameters
Return ValueReturns the index (0-based) of the variable if the function was successful, -1 if an error was encountered. Remarks
Example// Get the index of "MyVar" from CDataFile, df. int iVar = df.GetVariableIndex("MyVar"); if(iVar == -1) cout << df.GetLastError(); // Get the index of "My Var 2" occurring after // variable 3 whose source file is "C:\data.csv". int iVar = df.GetVariableIndex("My Var 2", "C:\\data.csv", 3); if(iVar == -1) cout << df.GetLastError(); CDataFile::GetVariableName()The int GetVariableName(const int& iVariable, char* lpStr); int GetVariableName(const int& iVariable, std::string& rStr); Parameters
Return ValueReturns the length of the variable name if the function was successful, -1 if an error was encountered. RemarksUse
Example// Get the variable name at index 3. CString szVarName = ""; int iLength = df.GetVariableName(3, szVarName.GetBuffer(0)); szVarName.ReleaseBuffer(); // or... std::string szVar = ""; iLength = df.GetVariableName(3, szVar); if(iLength==-1) cout << df.GetLastError(); else cout << szVar.c_str(); CDataFile::ReadFile()The bool ReadFile(const char* szFilename); bool ReadFile(const char* szFilename, const unsigned& dwReadFlags); Parameters
Return ValueReturns RemarksUse
Example// Read the contents of "C:\test.csv" into CDataFile, df. if(!df.ReadFile("C:\\test.csv")) cout << df.GetLastError(); // Append the data from "C:\test2.csv" to CDadaFile, df. if(!df.ReadFile("C:\\test2.csv", DF::RF_APPEND_DATA)) cout << df.GetLastError(); CDataFile::SetData()The bool SetData(const int& iVariable, const int& iSample, const double& value); bool SetData(const int& iVariable, const int& iSample, const char* szValue); Parameters
Return ValueReturns RemarksUse
Example// Set variable 3, sample 0 to 2.121 in CDataFile, df. if(!df.SetData(3,0,2.121)) cout << df.GetLastError(); CDataFile::SetDelimiter()The void SetDelimiter(const char* delim); Parameters
Example// Set the delimiter in CDataFile, df, to 'tab'. df.SetDelimiter("\t"); CDataFile::SetReserve()The void SetReserve(const int& nReserve); Parameters
Example// Set the reserve of CDataFile, df, to 1000000. df.SetReserve(1000000); CDataFile::WriteFile()The bool WriteFile(const char* szFilename, const char* szDelim = ","); Parameters
Return ValueReturns Remarks
Example// Write the contents of CDataFile, df, to "C:\test.csv". if(!df.WriteFile("C:\\test.csv")) cout << df.GetLastError(); CDataFile::operator()The double operator()(const int& iVariable, const int& iSample); int operator()(const int& iVariable, const int& iSample, char* lpStr); DF::DF_SELECTION operator()(const int& left, const int& top, const int& right, const int& bottom); Parameters
Return Value
RemarksUse
Example// Set d equal to the data at (2,4) from CDataFile, df. d = df(2,4); // Get the string data contained at (1,7). CString szData = ""; int iLength = df(1,7,szData.GetBuffer(0)); szData.ReleaseBuffer(); // Create a new CDataFile from the range (0,0,9,120). dfNew(df(0,0,9,120)); CDataFile::operator[][]The Return ValueReturns a reference to the data at the specified location. RemarksThis operator is only valid for
Example// Set d equal to the data at variable 2, sample 4 from CDataFile, df. double d = df[2][4]; // Set the data at variable 9, sample 0, from CDataFile, df, equal to d. df[9][0] = d; CDataFile::operator+The CDataFile operator+ (const CDataFile&) const; Example// Set CDataFile, df, to the combined data of df2, df3, and df4. df = df2 + df3 + df4; CDataFile::operator+=The CDataFile& operator+=(const CDataFile&);
Example// Append the contents of CDataFile, df2, to CDataFile, df. df += df2; CDataFile::operator=The CDataFile& operator =(const CDataFile&); CDataFile& operator =(const DF::DF_SELECTION&); Example// Set CDataFile, df2, equal to CDataFile, df. df2 = df1; // Set df3 equal to the selection df(0,0,4,120). df3 = df(0,0,4,120); CDataFile::operator<<The std::ostream& operator << (std::ostream&, const CDataFile&); std::ostream& operator << (std::ostream&, const DF::DF_SELECTION&); Example// Put CDataFile, df, to outStream. outStream << df; // Put the selection (0,0,5,15) to outStream. outStream << df(0,0,5,15); CDataFile::operator>>The std::istream& operator >> (std::istream&, CDataFile&); Example// Get CDataFile, df, from inStream. inStream >> df;
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||