If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Reading Binary Files in C++

I am having trouble taking values from byte locations in my file. The code below works for certain locations where you have to read 2-bytes, it does not seem to work with 4-bytes however. I believe there must be something wrong with my interpretation of how to read(char*, int) function reads binary data. What I'm looking for is the ability to read say 4 bytes of binary data, and store the decimal value/char value of each byte of data in an index of the array. That is my understanding of how the read function works. For one section of 4-byte code I should get an overall value of 1500, but instead I get 101 in the second index(1 starting from 0) which is incorrect. The other indices are zero using variable watch. I know should someone get an array that has 220 in the first index and 5 in the second index. After bit shifting this will evaluate to 1500. Any insight as to why my read function is not assigning the values to my array that I need?

Two other programs written by other people, (one in pascal one with no source code) read 1500 for this specific byte location so something must be off in my implementation of the read function.

For my test file data4.txt (on my Windows 7 system) which has those particular bytes set starting at offset 3213, this program displays the required value of 1500. If you try this program on your system, what value is displayed? If you zip up your program and the data file and attach it to a post I'll investigate further if there is still an issue.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: Reading Binary Files in C++

I am trying to read a binary file and extract the data. In the first part of the file there is an XML header with meta data. I haven't gotten to the part where I examine yet how to get past this and am working on the data itself for now.

Re: Reading Binary Files in C++

...and your question is? If the code is not producing the expected output, then would you attach a sample input file.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!