How are you reading it back out? The following code “works,” though it is incorrect and generates a warning. To generate the test file:
[code]{
const Int_t n = 2;
Char_t words[n][256] = {“one”, “two”};
TFile *fout = new TFile(“chartest.root”, “recreate”);
TTree *test = new TTree(“chartest”, “char test”);
test->Branch(“n”, &n, “n/I”);
test->Branch(“words”, words, “words[n]/C”);
for(int i=0;i < n;i++)
cout << words[i] << endl;
test->Fill();
test->Write();
}[/code]
Reading it out:
root [0] .x chartest.C
one
two
root [1] TFile *fin = new TFile("chartest.root", "READ");TTree *test = (TTree*)fin->Get("chartest");
Warning in <TFile::Init>: file chartest.root probably not closed, trying to recover
Info in <TFile::Recover>: chartest.root, recovered key TTree:chartest at address 390
Warning in <TFile::Init>: successfully recovered 1 keys
root [2] Char_t words[2][256];
root [3] test->SetBranchAddress("words", words)
(const Int_t)0
root [4] test->GetEntry()
(Int_t)8
root [5] words[0]
(Char_t* 0x17aaba0)"one"
root [6] words[1]
(Char_t* 0x17aaca0)"two"
I believe the correct code to create the output file is:
[code]{
const Int_t n = 2;
Char_t words[n][256] = {“one”, “two”};
TFile *fout = new TFile(“chartest.root”, “recreate”);
TTree *test = new TTree(“chartest”, “char test”);
test->Branch(“n”, &n, “n/I”);
test->Branch(“words”, words, “words[2][256]/C”);
for(int i=0;i < n;i++)
cout << words[i] << endl;
test->Fill();
test->Write();
fout->Close();
}[/code]
Then, read it in:
[code]{
TFile *fin = new TFile(“chartest.root”, “READ”);
TTree test = (TTree)fin->Get(“chartest”);
Char_t words[2][256];
test->SetBranchAddress(“words”, words);
test->GetEntry();
for(int i=0;i < 2;i++)
cout << words[i] << endl;
fin->Close();
}
[/code]
This works for me in ROOT 5.34.04. Both dimensions of the array should be specified when you construct the branch.