Binary data reading with root on vista

Hello all!

I’ve encountered the problem of binary data file reading. I use the root interpreter installed on Vista (all current updates done) and Ubuntu 8.04 — Hardy Heron.

The binary data file is the sequence of 512 byte of comments and 8192 integers with the length of 4 byte. I need only the comments and first 1024 integers.

The macros to read binary is:

{

gROOT->Reset();

union dun_t
{
Int_t i;
Char_t c[4];
}
dun;

Char_t head[512];
Float_t i,min=0,max=1024;

ifstream inp;
inp.open(“test”);
TH1F *hm = new TH1F(“000”,“test”,1024,min,max);
inp.read(head,512);
cout<<head<<endl;
for(i=0;i<1024;i++)
{
inp.read(dun.c,4);
hm->Fill(i,dun.i);
}
inp.close();

}

This macros executed on the Ubuntu machine works correctly. But in the Vista machine only first part of the file (first 800 byte) is reading properly and the rest are strange overflow with value about 1.5 billion.

Could someone help me to understand how to fix it?

[quote=“roman.vasilyev”]Hello all!

I’ve encountered the problem of binary data file reading. I use the root interpreter installed on Vista (all current updates done) and Ubuntu 8.04 — Hardy Heron.

The binary data file is the sequence of 512 byte of comments and 8192 integers with the length of 4 byte. I need only the comments and first 1024 integers.

The macros to read binary is:

{

gROOT->Reset();

union dun_t
{
Int_t i;
Char_t c[4];
}
dun;

Char_t head[512];
Float_t i,min=0,max=1024;

ifstream inp;
inp.open(“test”);
TH1F *hm = new TH1F(“000”,“test”,1024,min,max);
inp.read(head,512);
cout<<head<<endl;
for(i=0;i<1024;i++)
{
inp.read(dun.c,4);
hm->Fill(i,dun.i);
}
inp.close();

}

This macros executed on the Ubuntu machine works correctly. But in the Vista machine only first part of the file (first 800 byte) is reading properly and the rest are strange overflow with value about 1.5 billion.

Could someone help me to understand how to fix it?[/quote]
Try opening the file as binary stream rather than text.

Changing
inp.open(“test”);
with
inp.open(“test”, ios::in||ios::binary);
brings to the same result.

Still do not work.

Changing
inp.open(“test”);
with
inp.open(“test”, ios::in||ios::binary);
brings to the same result.

Still do not work.[/quote]
As we all know, there is no magic.
You could try:

  1. use binary stream to read the file.

  2. set default values for dun (it is a must, by the way) before using it in the read function.

for(i=0;i<1024;i++) { inp.read(dun.c,4); hm->Fill(i,dun.i); }

  1. check a stream status before using date returned by the read function. Don’t trust anyone, always check.

  2. you may want to read “unsigned char” instead of “char”, if your file is really a binary one.

  3. use well known friend of developers - a debugger.

Changing
inp.open(“test”);
with
inp.open(“test”, ios::in||ios::binary);
brings to the same result.

Still do not work.[/quote]
BTW, the call:

is wrong,
you should use a bitwise operator “OR”(|) and not a logical operator “OR” (||).:

And once again, check that the file is actually opened and there is no errors in the stream before doing anything with the stream.

Of course | rather than ||!

Now works ok. Thanks!