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
std::vector
// 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;
row.push_back(data);
}
table.push_back(row);
}
// Now let’s use the table
for (unsigned i=0; i
December 9, 2007 at 3:07 pm
I have been working with reading data files similar to this for some time. Instead of implementing my own code I found table_io (http://people.scs.fsu.edu/~burkardt/cpp_src/table_io/table_io.html)
offered all I needed.
As a matter of fact I did a C++ wrapper around table_io that works like a charm…
December 13, 2009 at 7:17 pm
Hi, I think this code looks great! But, when I try your data, here is my output:
hammer:~/hw2/p3 eliz$ ./a.out
New line of size: 9
0, 51, 0.00164916, -0.00770348, 73.0007, 40.7776, 214.276, 76.1719, 76.1719,
New line of size: 9
1, 51, 0.00164916, -0.00770348, 73.0007, 40.7777, 214.276, 76.1719, 76.1719,
New line of size: 9
2, 51, 0.00164916, -0.00770348, 73.0007, 40.7782, 214.276, 76.1719, 76.1719,
New line of size: 9
3, 51, 0.00164916, -0.00770348, 73.0007, 40.7795, 214.275, 76.1719, 76.1719,
New line of size: 1
December 14, 2009 at 6:13 pm
I think this fixes it:
if(!isdigit(file.peek())) {
file.ignore();
continue;
}
and similarly for the section below.
Thank you!
February 26, 2011 at 7:46 am
Your webpage is a very good summary of your activities. great!
Wiko looks like very interesting.
September 11, 2012 at 9:19 am
Unfortunately I get an error:
1. ” ‘amp’ declared as reference but not in initialized”
2. ” ‘row’ was not declared in this scope”
Can you maybe help me out?
January 15, 2015 at 9:50 pm
Read an input file that contains the flight details. Store the information for
each flight into a Flight class object. Create a vector of Flight objects. The
Flight objects should have string members for each column i.e. flightNo,
destination, departure and gateNo.
Can someone help or give me idea?