1

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 
4
  • It may be easier to use good old sscanf(). Or, change Energy to be std::string, then erase the last character (the -). Commented Jun 24, 2018 at 5:16
  • Please could you ellaborate? Commented Jun 24, 2018 at 5:20
  • It is a strange number notation to have a "-" following AFTER a floating point number. Is it unconditionally always there? Could it be a "+"? Could it be a " "? Commented Jun 24, 2018 at 5:35
  • You need to skip the first three lines because they are not in the expected format. Commented Jun 24, 2018 at 5:40

2 Answers 2

1

From what I understand of what you are trying to do I think you have two problems.

The first is you are reading the first three lines as if they were data but they are not, they are header information. So I think you need to just skip past the first three lines:

// Skip first three lines std::getline(f, line); std::getline(f, line); std::getline(f, line); 

The other is you need to read the dash '-' character between your class interval values.

So all in all something like this:

#include <iostream> #include <cmath> #include <fstream> #include <sstream> int main() { std::ifstream f("t2kflux.txt"); std::string line; // Skip first three lines std::getline(f, line); std::getline(f, line); std::getline(f, line); while(std::getline(f, line)) { float serial; double Energy, Energy2; double mu; double mubar; double e; double ebar; char dash; // to absorb the '-' separator std::istringstream ss(line); ss >> serial >> Energy >> dash >> Energy2 >> mu >> mubar >> e >> ebar; std::cout << Energy << "\t" << mu << "\n"; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! It worked for me exactly as I neede. I didn't know how to include dash!
@MasoomSingh dash is the name of a variable I added. It is just a char so you can read the separator into it.
1

I have written the following code and it is working fine for me. I hope this may help you.

#include<iostream> #include<cmath> #include<fstream> #include<string> #include<sstream> int main() { std::ifstream f("honey.txt"); std::string line; while(std::getline(f,line)) { float serial; std::string Energy,Energy2; std::string mu; std::string mubar; std::string e; std::string ebar; std::istringstream ss(line); ss>>serial>>Energy>>Energy2>>mu>>mubar>>e>>ebar; std::cout<<Energy<<"\t"<<mu<<"\n"; } } 

Other approach to solve this problem is:

Step 1: First create a program that removes dash(-) from the file.

Step 2: Use your program(as in question) and it will work correctly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.