Filling a Text file

I am trying to create a text file and then fill it. I have never done this before but I looked around and I got a text file created, however, it only puts the column labels. I want the first column to be filled with the variable A and the second one to be filled with the variable Z. Here is what I have:

void analysis(int index=0){

  TChain *chain = new TChain("EICTree"); //Au-197

  Int_t nevents = chain->GetEntries();

  //Create text file
  ofstream myfile;"Measured-Isotopes.txt");
  myfile << "A  Z\n";

  int A(0),Z(0);
   for(int j=0;j<nParticles;j++){
     particle_eA = particle->eA;
      A = particle_eA->massNum;
      Z = particle_eA->charge;
      if(id >= 80000 && Status == 1000){
        cout << A << "  " << Z << endl;
        myfile << A << "  " << Z << "\n";

I left out some stuff that doesn’t really relate to this issue. I am just having trouble filling the text file with the actual data.

Hi Zach,
I think that you should put the myfile.close(); outside the for loop.
With the close inside the loop you close the file at the first iteration, and you cannot write on it anymore.


Sorry that was a typo on my part. In my actual code it is outside the for loops. I just forgot to add another bracket so I put it a little early.

Do you enter in the if? e.g. A and Z are printed on the monitor?

On my laptop wit ROOT 6.20.08 I can open, write and close a file smoothly.
Otherwise I think is more a c++ related issue than a ROOT one.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.