Dear ROOTers,
Is it possible to save plain text in a ROOT file?
I have tried with a TName, but maybe it is not the best way of doing it.
Thank you for your time.
Regards,
ATD.
Dear ROOTers,
Is it possible to save plain text in a ROOT file?
I have tried with a TName, but maybe it is not the best way of doing it.
Thank you for your time.
Regards,
ATD.
Another option is TObjString.
Hello
Thank you, that was what I was looking for.
For anyone with this issue, please find attached a working example.
#include "TFile.h"
#include "TObjString.h"
#include <string>
#include <iostream>
void writeString( std::string & ostring )
{
TFile * ofile = TFile::Open("junk.root","recreate");
TObjString a( ostring.c_str() );
ofile->WriteObject( &a, "myStr" );
ofile->Close();
return;
}
void readString()
{
TFile * ofile = TFile::Open("junk.root","read");
TObjString * a;
ofile->GetObject( "myStr" , a);
ofile->ls();
std::cerr << "\n\t** String: " << a->GetString().Data() << std::endl;
ofile->Close();
return;
}
int saveString( std::string dummyString = "atd" )
{
writeString( dummyString );
readString();
return 0;
}
Regards,
ATD
Another option (especially for code) is TMacro.
Hi ATD,
nice reference for future Forum readers, thanks!
I add a last example, using directly std::string, just to complement your solution:
void writeString()
{
TFile ofile("myfile.root","recreate");
std::string a( "My very nice STL string" );
ofile.WriteObject( &a, "myStr" );
}
void readString()
{
TFile ifile("junk.root","read");
std::string *aptr;
ifile.GetObject( "myStr" , aptr);
auto &a = *aptr;
ifile.ls();
std::cerr << "\n\t** String: " << a << std::endl;
}
Cheers,
D
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.