0

Hi everyone i have an issue while reading binary data from a binary file as following:

File Content: D3 EE EE 00 00 01 D7 C4 D9 40

char * afpContentBlock = new char[10];
ifstream inputStream(sInputFile, ios::in|ios::binary);

if (inputStream.is_open()))
{
    inputStream.read(afpContentBlock, 10);

    int n = sizeof(afpContentBlock)/sizeof(afpContentBlock[0]); // Print 4

    // Here i would like to check every byte, but no matter how i convert the 
    // char[] afpContentBlock, it always cut at first byte 0x00.
}

I know this happens cause of the byte 0x00. Is there a way to manage it somehow ? I have tried to write it with an ofstream object, and it works fine since it writes out the whole 10 bytes. Anyway i would like to loop through the whole byte array to check bytes value.

Thank you very much.

2
  • 3
    The usual reply is "why aren't you using vector?" (and I'm quite certain sizeof(afpContentBlock)/sizeof(afpContentBlock[0]) is not 7, but 4 or 8). Commented Aug 8, 2012 at 11:01
  • I had fixed the file content (it was wrong).A code snippet would really be helpful and appreciated since i am really not used yet to vector. Commented Aug 8, 2012 at 12:11

1 Answer 1

2

It's much easier to just get how many bytes you read from the ifstream like so:

if (inputStream.is_open()))
{
   inputStream.read(afpContentBlock, 10);
   int bytesRead = (int)inputStream.gcount();

   for( int i = 0; i < bytesRead; i++ )
   {
      // check each byte however you want
      // access with afpContentBlock[i]
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Duncan_McCloud You're right, edited to fix that. Been a while lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.