ReadFile and strings

So, ReadFile() will put ascii into a tree using the standard branch definitions.
Strings are assumed to be terminated with a null character. So, I have multiple strings to read, and thusly, I added the “\0” to the end of the fields using awk. I can see the null characters with less “^@”.
However, ReadFile() reads in 0 lines. I have been playing around with the file and the descriptor, but I cannot get ReadFile to read more than one string per line.

You should not need to add the null character in your file.
Can you send your orignal try and how it failed.


If I remove the null characters, I get the same responce. But without some delimiter how will the ReadFile() now when a String ends?

TTree *tr=new TTree("test","test") tr->ReadFile("test.txt","run/C:type:day/I:month:year:hour:minute:second:events:good/C:soso:bad:not:dayb/I:monthb:yearb:hourb:minuteb:secondb:module/C")       

I went through the documentaction but it deadends at TLeaf leaf->ReadValue(in) claiming that the code for this fuction is {}.
test_with_nulls.txt (12.1 KB)
test.txt (9.48 KB)

I have been playing wiht this function. It appears to assume that a string ends at the fist sign of whitespace. If I put some intergers in the mix, it reads nothing because it trys to read the next set os characters as the integer. Can I change the delimiter? Can I group a String with brackets or something(I tried “” with no success)?

[quote]But without some delimiter how will the ReadFile() now when a String ends? [/quote]As you noticed, it is at the first white space. The relevant code is at: … tml#G_UspE
If you can find a better implementation, please feel free to send it to us :slight_smile:
[Note that I don’t think embedded null character in a text file is a good delimiter … I suspect the best improvement would be to allow quoting the string (with ")]


I have rewritten my awk script to replace whitespace with _'s. I would have done this to begin with if I would have known ReadFile()'s behavior (or avoided this function altogether).
Thank you for the link. I really need to look at this code to understand it, but if I come up with something, I will definitely send it in. I agree that whitespace as a delimiter is a much better default than an embedded null. I don’t think that " is the best solution either, but certainly better. Until someone thinks of something better, how about this adding the following to the documentation:

– Create or simply read branches from filename.

if branchDescriptor = “” (default), it is assumed that the Tree descriptor
is given in the first line of the file with a syntax like
otherwise branchDescriptor must be specified with the above syntax.
This function will read and ignore any whitespace characters (this includes blank spaces and the newline and tab characters) which are encountered before the next non-whitespace character. This includes any quantity of whitespace characters, or none.
-If the type of the first variable is not specified, it is assumed to be “/F”
-if the type of any other variable is not specified, the type of the previous
variable is assumed. eg
x:y:z (all variables are assumed of type "F"
x/D:y:z (all variables are of type "D"
x:y/D:z (x is type “F”, y and z of type “D”
-If the type is a string of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).

Lines in the input file starting with “#” are ignored.

A TBranch object is created for each variable in the expression.
The total number of rows read from the file is returned.

The language mirrors that of sscanf. It would have saved me alot of time. This text should appear in the user’s guide, chapter 12 Example 5, as well.
Thank you again without this forum, I would be lost. [/quote]


I agree :wink: This is now in CVS.