tags:

views:

69

answers:

3

I have a very strange problem when reading a binary file.

void metaDataProcess(FILE *f){

    unsigned __int32 obLength;
    unsigned __int32 numProp;
    char* objPath;
    unsigned __int32 rawDataIndex;
    int level;
    fread(&obLength,sizeof(obLength),1,f);
    objPath=new char[obLength];
    cout<<"i am at"<<ftell(f)<<endl;
    fread(&objPath,sizeof( char),obLength,f);
    objPath[obLength]='\0';
    cout<<"i am at"<<ftell(f)<<" the object path is "<<objPath<<endl;
level=getOrCreateNode(objPath);

fread(&rawDataIndex,sizeof(rawDataIndex),1,f);

the "objPath" didnt get what is expected in that location. In 010 editor, for that location it is '/', but i read it as '>'. it is quite strange, since from the print out value of ftell, it is the correct position and the value read before and after that are got expected value(obLength=1; and next value rawDataIndex==4294967295).

how come i got '>' when i expceted '/'. i tried fread(&objPath,sizeof(unsigned char),obLength,f); fread(&objPath,1, obLength,f); they are all '>'; can anyone help me with it? thanks

+1  A: 
objPath=new char[obLength + 1];
cout<<"i am at"<<ftell(f)<<endl;
fread(objPath,sizeof( char),obLength,f);
objPath[obLength]='\0';
anon
i tried. not working
Grey
@Grey The code is correct. If it doesn't work, your input is wrong. You should of course be testing whether fread() etc. succeeded or not.
anon
i found the problem. it was a typofread(= >fread(objPath,sizeof( char),obLength,f);thanks guys
Grey
+1  A: 

I can't see anything wrong with the code above, except that you are acessing an ilegal memory position, since you allocate:

objPath=new char[obLength];

and then do:

objPath[obLength]='\0';

You should have allocated new char[obLength+1] to reserve enough space for the '\0'.

The other thing is that you are printing the result of ftell after reading the file. Is that what you want really?

Gianni
i change it to obLength+1 right after i post this. but it is not working. i print 2 ftell before and after reading, in order to know if the correct length is read. it shows 36 and 37 in decimal. which are correct positions. it is quite strange why it just doesnt give me the correct string. i have the same code in C and it works fine for me. when i use this chunck of code in visual stduio 2010 vc++. it just not giving me the correct thing...
Grey
@Grey you mean that this doesn't work for gcc but works for VC++? Or do you mean that if you put this in a .c file, instead of a .cpp file it works?
Gianni
A: 

Is your 010 editor showing position in hex rather than decimal? You're programmatically printing it in decimal so that could account for the difference you're seeing.

EDIT: What does your file look like? Is the < one character off from the / you expect? Have you tried reading the characters one at a time and finding out which offset the / actually exists at?

Mark B
it can show both hex and decimal. before reading it shows 36, and after reading it show 37, which is the correct length i want. However it just gives me '>' instread of '/'
Grey