|
-
July 28th, 2017, 04:39 PM
#1
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.
Code:
unsigned char buf[4]; //{220, 5, 0, 0 }; ->values that my read function should be assigning
uint64_t buf1[4];
uint64_t value = readBufferLittleEndian(buf, &file, buf1);
cout << value;
system("PAUSE");
return 0;
}
uint64_t readBufferLittleEndian(unsigned char buf[], std::ifstream *file, uint64_t buf1[])
{
file->seekg(3213);
file->read((char*) buf, 4);
for (int index = 0; index < 4; index++) {
buf1[index] = static_cast<uint64_t>(buf[index]);
}
return ((uint64_t)buf1[3] << 24 | (uint64_t)buf1[2] << 16 |(uint64_t)buf1[1] << 8 | (uint64_t)buf1[0]);
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|