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;
row.push_back(data);
}
table.push_back(row);
}

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

Advertisements

6 Responses to “Reading data files in C++”

  1. xamat Says:

    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…

  2. liz Says:

    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

  3. liz Says:

    I think this fixes it:

    if(!isdigit(file.peek())) {
    file.ignore();
    continue;
    }

    and similarly for the section below.

    Thank you!


  4. Your webpage is a very good summary of your activities. great!

    Wiko looks like very interesting.

  5. Lazloo Says:

    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?

  6. nemanja Says:

    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?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: