Parse text files with columns. More...
#include <ReadAscii.h>
Public Member Functions | |
bool | GetCol (vector< string > &col, const int co) |
Dumps the content of a string column in a vector. | |
bool | GetCol (vector< double > &col, const int co) |
Dumps the content of a double column in a vector. | |
bool | GetCol (vector< int > &col, const int co) |
Dumps the content of an integer column in a vector. | |
bool | GetElement (string &element, const int li, const int co) |
Returns the string data value at a given line and column position. | |
bool | GetElement (double &element, const int li, const int co) |
Returns the double data value at a given line and column position. | |
bool | GetElement (int &element, const int li, const int co) |
Returns the integer data value at a given line and column position. | |
bool | GetLine (string &line, const int li) |
Dumps the content of a line as a string. | |
int | GetNCol (void) |
Returns the number of columns. | |
int | GetNLine (void) |
Returns the number of rows/lines. | |
int | GetNRow (void) |
Returns the number of rows/lines. | |
bool | SetFormat (const int co, const char aformat) |
Defines a new column format ('s', 'd' or 'i'). | |
Constructors and destructors | |
ReadAscii (const string afilename, const string aformat="UNKNOWN") | |
Constructor of the ReadAscii class. | |
virtual | ~ReadAscii (void) |
Destructor of the ReadAscii class. |
Parse text files with columns.
Many operations are then available to access and modify the data elements.
ReadAscii::ReadAscii | ( | const string | afilename, | |
const string | aformat = "UNKNOWN" | |||
) |
Constructor of the ReadAscii class.
With this constructor, the text file is loaded and data are extracted. The text file must be composed of a fix number of data columns. This file should not contain more than 50 columns. Columns should be separated by white spaces. Empty lines and commented lines ('//', '#' or '%') are ignored. If known, the format of each column should be provided in argument. The format syntax is the folowing: \"t;t;t\", where ';' separates each column definition and 't' refers to the type of data which is expected. Three formats are supported:
{.cpp} ReadAscii("/path/to/my/file.txt", "s;s;i;i");
This example means that the text file is composed of 4 columns, the 2 first ones are string fields and the 2 last ones are integer fields.
If the input format is unknown, aformat should be set to "UNKNOWN" which it is by default. In that case, all columns are assumed to have a string format. This default format can be changed later on by using the SetFormat() function.
afilename | path to the text file | |
aformat | string format to be expected in the file |
ReadAscii::~ReadAscii | ( | void | ) | [virtual] |
Destructor of the ReadAscii class.
bool ReadAscii::GetCol | ( | vector< string > & | col, | |
const int | co | |||
) |
Dumps the content of a string column in a vector.
true is returned in case of success.
col | returned vector of string data. | |
co | selected column number (indexing starts at 0). |
bool ReadAscii::GetCol | ( | vector< double > & | col, | |
const int | co | |||
) |
Dumps the content of a double column in a vector.
true is returned in case of success.
col | returned vector of double data | |
co | selected column number (indexing starts at 0) |
bool ReadAscii::GetCol | ( | vector< int > & | col, | |
const int | co | |||
) |
Dumps the content of an integer column in a vector.
true is returned in case of success.
col | returned vector of integer data | |
co | selected column number (indexing starts at 0) |
bool ReadAscii::GetElement | ( | string & | element, | |
const int | li, | |||
const int | co | |||
) |
Returns the string data value at a given line and column position.
true is returned in case of success.
element | returned data value. | |
co | selected column number (indexing starts at 0). | |
li | selected line number (indexing starts at 0). |
bool ReadAscii::GetElement | ( | double & | element, | |
const int | li, | |||
const int | co | |||
) |
Returns the double data value at a given line and column position.
true is returned in case of success.
element | returned data value. | |
co | selected column number (indexing starts at 0). | |
li | selected line number (indexing starts at 0). |
bool ReadAscii::GetElement | ( | int & | element, | |
const int | li, | |||
const int | co | |||
) |
Returns the integer data value at a given line and column position.
true is returned in case of success.
element | returned data value. | |
co | selected column number (indexing starts at 0). | |
li | selected line number (indexing starts at 0). |
bool ReadAscii::GetLine | ( | string & | line, | |
const int | li | |||
) |
Dumps the content of a line as a string.
true is returned in case of success.
line | returned line. | |
li | selected line number (indexing starts at 0) |
int ReadAscii::GetNCol | ( | void | ) | [inline] |
Returns the number of columns.
int ReadAscii::GetNLine | ( | void | ) | [inline] |
Returns the number of rows/lines.
int ReadAscii::GetNRow | ( | void | ) | [inline] |
Returns the number of rows/lines.
bool ReadAscii::SetFormat | ( | const int | co, | |
const char | aformat | |||
) |
Defines a new column format ('s', 'd' or 'i').
The selected column is converted to a new format. Only one conversion is destructive: 'd' to 'i'. In that case, the doubles are recasted as integers. true is returned in case of success.
co | selected column number (indexing starts at 0). | |
aformat | char new format ('s', 'd' or 'i'). |