Reading Binary files

Hi,

I am trying to write the code to read binary files which is generated by other system say X
X generated the binary files using cobol code. it consists of COMP fields.

When I am trying to read the these fields it is not generating any thing.

I am reading into character buffer like

if1.read(buffer,9);
cout<<"YYY="<<atoi(buffer)<<endl;
it is happening for some fields.

do i need to take any extra measures to read this type fields.

Please suggest



Thanks
fstream::read doesn't read null terminated string into buffer. It reads raw data, but atoi() requires a null terminated string.
Thanks for the reply.

if in the file they are 8 bytes of signed it
if1.read(buffer,8);
buffer[8]='\0'
cout<<"Integer value="<<atoi(buffer);
or
cout<<"Integer value="<<atol(buffer);

works ?

both are not working.

If it's a binary file, then why store numbers in a character format?
If they are in character format, then why don't you read it as a normal text file?

If you still want to read it as a binary file then :
Did you open the file with std::ios_base::binary flag set?

Output the buffer without first converting it with atoi(). You'll most likely see, that it is not in a number format ( like "12345678" ).
thanks i am able to read
Topic archived. No new replies allowed.