Re: Writing and reading TObString

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue May 18 1999 - 18:06:00 MEST


Hi Michal,
You had several problems in your macro. see my comments in the modified
macro below.

Rene Brun


void michal2()
{
// Create a new ROOT file to contain TObjString

    TFile *tfile = new TFile("michal2.root", "recreate");
    Char_t  large_str[10000];

    TObjString  Comment;
    large_str[0] = '\0';
    sprintf(large_str, "testing TObjString");
    Comment.SetString(large_str);  //use . instead of ->
    Comment.Print();
    Comment.Write();
 //   Comment -> Delete();   //you should not Delete a stack object
    tfile -> Write("Comment");

    tfile -> Close();
    delete tfile;

    TObject *obj;
    TKey *key;

    TFile *f = new TFile("michal2.root", "read");
    f -> ls();

 // create an iterator to loop through all objects(keys) in the  file
   TIter nextkey(f -> GetListOfKeys());
   while (key = (TKey*)nextkey()) {
       obj = key -> ReadObj();  //use ReadObj, not Read
       
       if (obj->InheritsFrom("TObjString") ) {  //instead of obj->IsA
          TObjString *Comm = (TObjString *) obj;
          Comm->Print();
       }
   }
   f -> Close();
}



lijowski@cosray2.wustl.edu wrote:
> 
>    Hello,
> 
>    I am trying to write a text into a root file and read it. Below
>    is a short macro which is based on Example of macro to add histogram files
>    in ROOT page. It crashes in line 41 with a message illegal pointer to
>    class object obj 0x0 .... .
> 
>    Am I doing something wrong or is it any other solution?
> 
>    Thank you and regards,
> 
>    Michal Lijowski
> 
> -------------------------------------------------------------------------
> 
> void test_TObjString()
> {
> //   gROOT -> Reset();
> 
>    Char_t  Macro_name[80] = "test_TObjString";
>    Char_t  infile[200], outfile[200];
> 
> // Create a new ROOT file to contain TObjString
> 
>     sprintf(outfile, "%s.root", Macro_name);
>     printf("%s\n", outfile);
> 
>     TFile *tfile = new TFile(outfile, "recreate");
>     Char_t  large_str[10000];
> 
>     TObjString  Comment;
>     large_str[0] = '\0';
>     sprintf(large_str, "testing TObjString");
>     Comment -> SetString(large_str);
>     Comment -> Print();
>     Comment -> Write();
>     Comment -> Delete();
>     tfile -> Write("Comment");
> 
>     tfile -> Close();
> 
>     TObject *obj;
>     TKey *key;
> 
>     sprintf(infile, "%s.root", Macro_name);
>     printf("%s\n", infile);
>     TFile *f = new TFile(infile, "read");
>     f -> ls();
> 
>  // create an iterator to loop through all objects(keys) in the  file
>    TIter nextkey(f -> GetListOfKeys());
>    while (key = (TKey*)nextkey()) {
>        obj = key -> Read();
>        if (obj -> IsA() -> InheritsFrom("TObjString") ) {
>           TObjString *Comm = (TObjString *) obj;
>           Comm -> Print();
>        }
>    }
> 
>    f -> Close();
> }



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:33 MET