Problem with reading negative integers from csv file

Dear I cannot read a large data file correctly. It contains many negative value but my code read only positive values. Here I attach my code:

[code]TFile *temp=new TFile(“adriana.root”, “RECREATE”);
ntuple = new TNtuple(“ntuple”,“NTUPLE”,“p”);
FILE *fp = fopen(“T188_46.csv”,“r”);
float p;
char line[127];
while (fgets(&line,127,fp)) {
sscanf(&line[0],"%f",&p);
printf("%f", p);
ntuple->Fill§;
}
TCanvas *c1_2 = new TCanvas(“c1_2”,“momentum background”,200,200,500,500);
ntuple->Draw(“p”);

temp->Write(); [/code]

Original file is very large (1.14GB), I cannot upload it. Here I am giving a small similar file.
Filehttp://www.filedropper.com/test_101
The expected graph is like this (dy.png), that is, the peak should be at 0 and other positive and negative number is equally distributed. But the obtained graph by the above code is different. Looking for your kind co-operation. Thanks in advance.


Did you ever look at your csv file? It looks like this (see below) - no need to download MB of data!

In particular, this csv file contains multi-row columns in doublequotes. The next question is which values you want to read and display. It seems you want the first column. In that case you need to check the number of double quotes. I.e. read the first field, then (in this case) read until you encounter the " at the end of a line.

And by the way: in C++ this line:

should fail to compile. fgets takes a pointer to a char, not to an array. I.e. use &line[0]. You can also use the fact that arrays decay to pointers -> fgets(line, …) (no &)

2.8340000000000032,"1 2.834 20 0.000 28 1.181 35 1.966 119 1.981 141 2.612 152 4.186 160 1.483 177 3.662 198 -1.114 229 -0.688 300 -0.237 326 -1.549 342 -1.449 343 0.224 393 -1.556 410 4.116 425 -3.674 433 -0.204 495 -0.684 Name: 21.623, dtype: float64" 0.0,"1 2.834 20 0.000 28 1.181 35 1.966 119 1.981 141 2.612 152 4.186 160 1.483 177 3.662 198 -1.114 229 -0.688 300 -0.237 326 -1.549 342 -1.449 343 0.224 393 -1.556 410 4.116 425 -3.674 433 -0.204 495 -0.684 Name: 21.623, dtype: float64"

I have ran your progam on a subset of your file and seems to me the first column is read correctly.
This only problem I see is that the entry before each line starting by “Name” is counted twice.

Thanks for your kind response. Now what I can do? How can I will get the expected result (graph)? Would you please help me a little more.

Yes! I found the solution. Here is the right code:

[code]std::ifstream inputFile(“T188_46.csv”);
std::string line;
TH1F *h1=new TH1F (“h1”,“Elpsarea Histo”,100,-400,400);
while(getline(inputFile, line)) {
if (!line.length() || line[0] == ‘#’)
continue;
std::istringstream iss(line);
double x = 0., y = 0.;

  iss>>x>>y;
  //std::cout<<"point: "<<y<<std::endl;
 h1.Fill(y); 

}[/code]

Thanks again for your kind co-operation.