Reading from .txt file (numbers and characters)

I have a text (.txt) file, that I generated in a macro using . It looks something like that:

"Histograms" "p0" "p1" "p2" ... "h1" val1.0 val1.1 val1.2 ... "h2" val2.0 val2.1 val2.1 ... ... ... ... ... ...
The first line contains the titles of the columns, and the first column, the name of my histograms. The numerical values are here represented by “val-something” (in the .txt file, they actually are numbers). Each entry in the text file is separated by a simple tabulation “\t”.
How do I use to get only the numerical values?
For each histogram (hence for each line of the .txt file), I have an array, and each cell of this array has to be filled with the corresponding value of “p”. In the end, I want to get:
array1 = {val1.0, val1.1, val1.2, etc.}
array2 = {val2.0, val2.1, val2.2, etc.}
and so on.

Do something like that

ifstream ifile("youfile.txt",ifstream::in); stringstream line; float val1, val2, ... ; getline(ifile,line.str()); //to skip the first line; while(getline(ifile,line.str())) { line >> dummy >> val1 >> val2 >> ...; } ifile.close();

What is “dummy” in your code?

dummy is a string variable. It is used to put the value of your first column (“h1”, “h2”, …)

Oh ok. But I need to declare it somewhere, don’t I?

Yes you have to declare it before.
Just add

before float val1, val2, …;

It’s still not working properly: I do get something in the arrays, but not what I expect. I get numerical values that don’t make sense.

I’ve attached a sample file. If you take a look at it, what I want to do is fill my arrays “array_r_minus_p”, “array_r_minus_pt” and so on, with the corresponding values on the line, and get "array_r_minus_p = {1.09098, 0.0120263, -0.00232262, etc.}
However, I get things like 6.94393e-310, 0 or even -nan. I guess there’s something I’m doing wrong.
fit_parameters.txt (656 Bytes)

Try to replace “float” with “double”.

Yeah, I already did that. My arrays are defined as Double_t.

[code]#include
#include

const int np = 6; // number of parameters
double r_minus_p[np];
double r_minus_pt[np];
double r_minus_eta[np];
double r_plus_p[np];
double r_plus_pt[np];
double r_plus_eta[np];
double r_soft_p[np];
double r_soft_pt[np];
double r_soft_eta[np];

void fit_parameters(const char *fname = “fit_parameters.txt”)
{
int i;
std::string stmp;

if (!(fname && *fname)) return; // just a precaution
std::ifstream ifs(fname, std::ifstream::in);
if (!ifs.is_open()) return; // just a precaution

std::getline(ifs, stmp); // skip the first line
ifs >> stmp; for (i = 0; i < np; i++) ifs >> r_minus_p[i];
ifs >> stmp; for (i = 0; i < np; i++) ifs >> r_minus_pt[i];
ifs >> stmp; for (i = 0; i < np; i++) ifs >> r_minus_eta[i];
ifs >> stmp; for (i = 0; i < np; i++) ifs >> r_plus_p[i];
ifs >> stmp; for (i = 0; i < np; i++) ifs >> r_plus_pt[i];
ifs >> stmp; for (i = 0; i < np; i++) ifs >> r_plus_eta[i];
ifs >> stmp; for (i = 0; i < np; i++) ifs >> r_soft_p[i];
ifs >> stmp; for (i = 0; i < np; i++) ifs >> r_soft_pt[i];
ifs >> stmp; for (i = 0; i < np; i++) ifs >> r_soft_eta[i];

ifs.close();
}[/code]