ReadAscii Class Reference

Parse text files with columns. More...

#include <ReadAscii.h>

List of all members.

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.

Detailed Description

Parse text files with columns.

Many operations are then available to access and modify the data elements.

Author:
Florent Robinet

Constructor & Destructor Documentation

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:

  • 's' for a string type
  • 'i' for a int type
  • 'd' for a double/float type
     {.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.

Parameters:
afilename path to the text file
aformat string format to be expected in the file
ReadAscii::~ReadAscii ( void   )  [virtual]

Destructor of the ReadAscii class.


Member Function Documentation

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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
co selected column number (indexing starts at 0).
aformat char new format ('s', 'd' or 'i').

The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Friends Defines

Generated on 29 Sep 2016 by  doxygen 1.6.1