I wanted to read this text file which has 6 columns. The problem is in the second column the input is in form of class interval and I don't know how to read that,the last I tried isn't doing the job. Moreover I wanted to print the second column and third but it's doing something else. Any help please.
#include <iostream> #include <cmath> #include <fstream> #include <sstream> int main() { std::ifstream f("t2kflux.txt"); std::string line; while (std::getline(f, line)) { float serial; double Energy, Energy2; double mu; double mubar; double e; double ebar; std::istringstream ss(line); ss >> serial >> Energy >> Energy2 >> mu >> mubar >> e >> ebar; std::cout << Energy << "\t" << mu << "\n"; } } The data sample is
Flux (/cm^2/1E21p.o.t/50MeV) Bin# Energy (GeV) numu anti-numu nue anti-nue --------------------------------------------------------------------------------- 1 0.00- 0.05 1.27e+04 1.64e+04 1.80e+02 2.97e+01 2 0.05- 0.10 5.40e+04 5.41e+04 1.02e+03 1.23e+02 3 0.10- 0.15 1.11e+05 3.98e+04 2.17e+03 2.12e+02 4 0.15- 0.20 1.75e+05 3.10e+04 3.23e+03 2.86e+02 5 0.20- 0.25 2.62e+05 2.78e+04 4.08e+03 3.56e+02 6 0.25- 0.30 3.68e+05 2.77e+04 4.73e+03 4.15e+02 7 0.30- 0.35 4.83e+05 2.83e+04 5.27e+03 4.61e+02 8 0.35- 0.40 5.97e+05 2.92e+04 5.58e+03 4.93e+02 9 0.40- 0.45 7.21e+05 2.97e+04 5.80e+03 5.18e+02 10 0.45- 0.50 8.89e+05 2.93e+04 5.77e+03 5.31e+02 11 0.50- 0.55 1.11e+06 2.93e+04 5.72e+03 5.35e+02 12 0.55- 0.60 1.24e+06 2.98e+04 5.53e+03 5.31e+02 13 0.60- 0.65 1.23e+06 2.92e+04 5.33e+03 5.26e+02 14 0.65- 0.70 1.14e+06 2.80e+04 5.03e+03 5.15e+02 15 0.70- 0.75 9.10e+05 2.59e+04 4.76e+03 5.05e+02
sscanf(). Or, changeEnergyto bestd::string, then erase the last character (the-).