Reading Binary Files in C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Reading Binary Files in C++

  1. #1
    Join Date
    Jul 2017
    Posts
    1

    Question 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]);
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,969

    Re: Reading Binary Files in C++

    Did you try to debug (step-by-step) your code to see where, how, and why something goes wrong?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,787

    Re: Reading Binary Files in C++

    Consider
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    const string binnam = "data4.txt"s;
    
    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]);
    }
    
    int main()
    {
    	ifstream file(binnam, ios::binary);
    
    	if (!file.is_open()) {
    		cout << "Cannot open file " << binnam << endl;
    		return 1;
    	}
    
    	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;
    }
    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. 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/

    C++17 Compiler: Microsoft VS2017 (15.4.1)

  4. #4
    Join Date
    Aug 2017
    Posts
    24

    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.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    
    int main () {
            ifstream file ("2009-02-09B.dat", ios::in|ios::binary);
    
            char ch;
            char level[4];
            char thetime[4];
            if(file.is_open())
            {
             while (!file.eof()) {
                    file.get(level);
                    file.get(ch);
                    file.get(thetime);
                    cout << (float) level << endl;
                    cout << ch << endl;
                    cout << (long) thetime << endl;
             }
              file.close();
    
            }
            else cout << "Unable to open file";
            return 0;
    }
    Last edited by VictorN; August 19th, 2017 at 05:03 AM. Reason: adding CODE tags

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,787

    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. 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/

    C++17 Compiler: Microsoft VS2017 (15.4.1)

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)