Reading data files in C++

December 3, 2007

Text processing in Python is so easy that I don’t feel like doing such kind of programming in C++ at all. However, sometimes I don’t have the choice. For example now I just had to implement a CLAM Processing that loads a big table of float data from a text file. Specifically, this is done in the processing ConcreteConfigure method.

What annoys me more of C++ file streams is the time I spend understanding its API. Let alone remembering it! So since I’ve just implemented a quite generic solution that loads a table of floats let’s blog about it and keep it at hand.

The input file. Actually, the file format is very flexible in terms of separators and the number of columns per row. The only requirement is that everything can be read as a float.

0 51.0 0.00164916 -0.00770348 73.0007 40.7776 214.276 76.1719
1 51.0 0.00164916 -0.00770348 73.0007 40.7777 214.276 76.1719
2 51.0 0.00164916 -0.00770348 73.0007 40.7782 214.276 76.1719
3 51.0 0.00164916 -0.00770348 73.0007 40.7795 214.275 76.1719

And this is the C++ code that reads the file in a data structure and then uses it:

#include ;
#include ;
#include ;
#include ;

// Data type where to load the table
typedef std::vector Row;
std::vector table;

// Load table from file
std::ifstream file(“/tmp/data”);
while (file)
std::string line;
std::getline(file, line);
std::istringstream is(line);
Row row;
while (is)
float data;
is >> data;

// Now let’s use the table
for (unsigned i=0; i