Login  Register

Re: How to read 'results' fromat in C++

Posted by dscho on Aug 04, 2011; 10:34pm
URL: http://imagej.273.s1.nabble.com/How-to-read-results-fromat-in-C-tp3683616p3683617.html

Hi Pei,

On Thu, 4 Aug 2011, Pei wrote:

>      I want to use the C++ program to read the results.txt of ImageJ. But after load the data seem so strange: Here is the C++ code :

You mean Results.txt with an upper-case 'R'. Even if you're on Windows, it
is a good habit to get into to pay attention to the case of the letters.

>     int main() {
>
> double Length, Width, LStrip, WStrip, NLStrip, NWStrip, NMeshLstrip, NMeshWstrip;
> double test[48];
> if(!experData){
>    cout << "Unable to open myfile";
>          exit(1); // terminate with error
>
>  }
> while (! experData.eof()){
> experData>>Length>>test[1]>>Width>>LStrip>>WStrip>> NLStrip>>NWStrip;
> cout <<NWStrip<<"\n";
>
> }
>  But the Output of NWStrips is 5.56182e-308. But I do not have this data in my files. How to read the" results" format file? Thank you

That number sounds awfully close to the smallest positive value you can
have with double precision floating points according to the IEEE standard:

        http://en.wikipedia.org/wiki/IEEE_754-2008#Basic_formats

Anyway, I gave it a try and this is what I came up with:

-- snipsnap --
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

std::istream& operator>>(std::istream& input_stream, std::vector<std::vector<double> >& table)
{
        std::string line;
        table.clear();

        while (std::getline(input_stream, line, '\n')) {
                std::istringstream parse_line(line);
                std::vector<double> row;
                std::string field;
                double value;

                while (std::getline(parse_line, field, '\t')) {
                        std::istringstream parse_field(field);
                        parse_field >> value;
                        row.push_back(value);
                }
                if (row.size() > 0)
                        table.push_back(row);
        }

        return input_stream;
}

int main(int argc, char **argv)
{
        std::ifstream input_stream(argv[1]);
        std::vector<std::vector<double> > table;

        input_stream >> table;

        std::cout << table.size() << " rows" << std::endl;

        for (int row = 0; row < table.size(); row++) {
                std::cout << "Row " << row << ": ";
                std::vector<double>& values = table[row];
                for (int column = 0; column < values.size(); column++)
                        std::cout << (column > 0 ? "; " : "") << values[column];
                std::cout << std::endl;
        }

        return 0;
}