Getting Zeros When Reading A File Full of Numbers [closed]

You can do this with std::ifstream

note This code doest assume that the input file is always nicely formatted and no values are missing on one rule

#include <fstream> //for ifstream
#include <string> //for strings

ifstream stream ( "tmp.txt", ios::in );
string type;
float dx;
float dy;
float intensity;
int nsat;
float rmsy;
float rmsx;

while ( stream >> type){
    stream >> dx;
    stream >> dy;
    stream >> intensity;
    stream >> rmsy;
    stream >> rmsx;

    cout << type << '\t'
        << dx << '\t'
        << dy << '\t'
        << intensity <<'\t'
        << rmsy << '\t'
        << rmsx << endl;
}

and with input.txt =

 R 111.1111 222.2222 123456 11 50.111
 T 111.1111 222.2222 123456 11 50.111

this prints this out again, note this is more idiomatic C++.

output =

R   111.111 222.222 123456  11  50.111
T   111.111 222.222 123456  11  50.111

Browse More Popular Posts

Leave a Comment