Fatal error: file '/usr/include/c++/5/fstream' modified since it was first

Dear Expert,
I extracted data from a root file and covert it to txt file in root v5 by use of the following commands:

#include <string>
#include <fstream>
using namespace std;
ofstream myfile;
myfile.open ("cst52.txt");
TFile * inputfile = TFile::Open( "cst5.root");
TTree * yourtree = (TTree*) inputfile->Get("Singles;4");
float Energy; // EDIT:you may actually want to change double to float because that is usually the branch type
yourtree->SetBranchAddress("energy", &Energy);
for (int i=0;i<yourtree->GetEntries();++i){
yourtree->GetEntry(i);
myfile << Energy << "\n";
}

Unfortunately, this code don’t work in root v6 and I have some errors as following:

fatal error: file '/usr/include/c++/5/fstream' modified since it was first
      processed
root [5] 
root [5] myfile.open ("cst52.txt");
input_line_55:2:3: error: use of undeclared identifier 'myfile'
 (myfile.open("cst52.txt"))
.
.
input_line_871:2:3: error: use of undeclared identifier 'myfile'
 (myfile << *(float*)0x7fb82c6435c0 << "\n")
  ^
input_line_872:2:3: error: use of undeclared identifier 'myfile'
 (myfile << *(float*)0x7fb82c6435c0 << "\n")
  ^
input_line_873:2:3: error: use of undeclared identifier 'myfile'
 (myfile << *(float*)0x7fb82c6435c0 << "\n")

Any help would be greatly appreciated.


ROOT Version: root_v6.02
Platform: Ubuntu
Compiler: Not Provided


try this file: tree2txt.C (516 Bytes)

$ root tree2txt.C
   ------------------------------------------------------------
  | Welcome to ROOT 6.15/01                  https://root.cern |
  |                               (c) 1995-2018, The ROOT Team |
  | Built for macosx64 on Oct 17 2018, 14:31:36                |
  | From heads/master@v6-13-04-1780-g99c0a2552e                |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] 
Processing tree2txt.C...
root [1] 

Thanks for your reply.
I have following error:

p@ubuntu:~/Desktop/root$ root tree2txt.C

| Welcome to ROOT 6.02/08 http://root.cern.ch |
| © 1995-2014, The ROOT Team |
| Built for linuxx8664gcc |
| From tag v6-02-08, 13 April 2015 |

Try ‘.help’, ‘.demo’, ‘.license’, ‘.credits’, ‘.quit’/’.q’

root [0]
Processing tree2txt.C…
fatal error: file ‘/usr/include/c++/5/fstream’ modified since it was first
processed
root [1]

Weird … seems there is something wrong with thefstream include on you machine

May be you can try the C way:

   FILE myfile;
   myfile = fopen ("cst52.txt","w");

Should I recompile root on my machine?

May be try a more recent version … 6.02 is old.

:roll_eyes:

p@ubuntu:~/Desktop/root$ root tree2txt.C
   ------------------------------------------------------------
  | Welcome to ROOT 6.02/08                http://root.cern.ch |
  |                               (c) 1995-2014, The ROOT Team |
  | Built for linuxx8664gcc                                    |
  | From tag v6-02-08, 13 April 2015                           |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] 
Processing tree2txt.C...
In file included from input_line_24:1:
/home/p/Desktop/root/tree2txt.C:8:11: error: no viable overloaded '='
   myfile = fopen ("cst52.txt","w");
   ~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/libio.h: note: candidate function (the implicit copy assignment
      operator) not viable: no known conversion from 'FILE *' (aka '_IO_FILE *')
      to 'const _IO_FILE' for 1st argument; dereference the argument with *
fatal error: file '/usr/include/libio.h' modified since it was first processed
root [1]

As I said you are using the old version 6.02 … I am using master version 6.15 … try to get the last released version 6.14

any way can you post the script file you wrote using fopen ?

I just replace
ofstream myfile;
myfile.open (“cst52.txt”);

with

   FILE myfile;
   myfile = fopen ("cst52.txt","w");
void tree2txt()
{
   FILE *myfile;
   myfile = fopen ("cst52.txt","w");

   for (int i=0; i<10; ++i) {
      fprintf(myfile,"%d\n",i);
   }
}

gives:

$ root tree2txt.C 
   ------------------------------------------------------------
  | Welcome to ROOT 6.15/01                  https://root.cern |
  |                               (c) 1995-2018, The ROOT Team |
  | Built for macosx64 on Oct 17 2018, 14:31:36                |
  | From heads/master@v6-13-04-1780-g99c0a2552e                |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] 
Processing tree2txt.C...
root [1] .q
$ cat cst52.txt 
0
1
2
3
4
5
6
7
8
9

Its OK.

p@ubuntu:~/root$ root tree2txt.C

| Welcome to ROOT 6.02/08 http://root.cern.ch |
| © 1995-2014, The ROOT Team |
| Built for linuxx8664gcc |
| From tag v6-02-08, 13 April 2015 |

Try ‘.help’, ‘.demo’, ‘.license’, ‘.credits’, ‘.quit’/’.q’

root [0]
Processing tree2txt.C…
root [1] .q
p@ubuntu:~/root$ cat cst52.txt
0
1
2
3
4
5
6
7
8
9
p@ubuntu:~/root$

With:

#include
#include
using namespace std;

void tree2txt()
{
FILE *myfile;
myfile = fopen (“cst52.txt”,“w”);

for (int i=0; i<10; ++i) {
fprintf(myfile,"%d\n",i);
}

TFile * inputfile = TFile::Open( “cst5.root”);
TTree * yourtree = (TTree*) inputfile->Get(“Singles;4”);
float Energy; // EDIT:you may actually want to change double to float because that is usually the branch type
yourtree->SetBranchAddress(“energy”, &Energy);
for (int i=0; iGetEntries();++i) {
yourtree->GetEntry(i);
myfile << Energy << “\n”;
}
}

I have:

p@ubuntu:~/root$ root tree2txt.C

| Welcome to ROOT 6.02/08 http://root.cern.ch |
| © 1995-2014, The ROOT Team |
| Built for linuxx8664gcc |
| From tag v6-02-08, 13 April 2015 |

Try ‘.help’, ‘.demo’, ‘.license’, ‘.credits’, ‘.quit’/’.q’

root [0]
Processing tree2txt.C…
In file included from input_line_24:1:
/home/p/root/tree2txt.C:20:14: error: invalid operands to binary expression
(‘FILE *’ (aka ‘_IO_FILE *’) and ‘float’)
myfile << Energy << “\n”;
~~~~~~ ^ ~~~~~~
root [1]

note the different way you should use to print … fprintf …

When i typed commands in the terminal separately, error only appear in the “for loop”:
for (int i=0; iGetEntries();++i) {
yourtree->GetEntry(i);
myfile << Energy << “\n”;

root [12] for (int i=0; iGetEntries();++i) {
root (cont’ed, cancel with .@) [13] yourtree->GetEntry(i);
root (cont’ed, cancel with .@) [14] myfile << Energy << “\n”;
root (cont’ed, cancel with .@) [15] }
ROOT_prompt_14:1:8: error: invalid operands to binary expression (‘FILE *’
(aka ‘_IO_FILE *’) and ‘float’)
myfile << Energy << “\n”;
~~~~~~ ^ ~~~~~~
root [16]

look at my code you should use fprintf … not <<
does my code works for you ? just the lilt macro I posted. If yes take it as model.

Thanks so much.

By use:
for (int i=0; iGetEntries();++i) {
yourtree->GetEntry(i);
fprintf (myfile,"%d\n",Energy);
}
I have error.

But, by using:
fprintf (myfile,"%f\n",Energy);
I have txt output with decimal numbers (%f):
0.180701
0.546286
0.134063
.
.
.

Whereas, true numbers are integer.

In your macro “Energy” is declared a float:

float Energy; // EDIT:you may actually want to change double to float because that is usually the branch type

Dear Olivier Couet,
Thanks for your response.
I got it. Everything is OK after using of the released version 6.14.
Thanks so much.
Best Regards