How to read 'results' fromat in C++

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to read 'results' fromat in C++

chenyanpei
Hello everyone !
     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 :
    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
Reply | Threaded
Open this post in threaded view
|

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

dscho
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;
}
Reply | Threaded
Open this post in threaded view
|

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

chenyanpei
Thanks for your advice !It works well in My eclipse. I will remember
the capitalization  "R ". You are good at stream of C++. Thank you
again!


2011/8/5 Johannes Schindelin <[hidden email]>

> 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;
> }
>



--
Chen Yanpei, phD
MssMat
Ecole Centrale Paris
Grande voie des vignes
92295 Châtenay Malabry
Paris, France