Reading binary data into struct with ifstream

Dan

I'm trying to read binary data from a file using ifstream.

Specifically, I'm trying to populate this "Header" struct with data read from a file:

struct Header {
    char id[16];
    int length;
    int count;
};
  1. Now, if I read the file in this way, the result is exactly what I want:

    input.read((char*)&hdr, sizeof(hdr));
    
  2. But if I instead read each variable of the struct manually, the results are gibberish:

    input.read((char*)&hdr.id,     sizeof(hdr.id));
    input.read((char*)&hdr.length, sizeof(hdr.length));
    input.read((char*)&hdr.count,  sizeof(hdr.count));
    

My question is, what is happening here that makes these two methods return different results?

Blaz Bratanic

As the comment above states, you are probably missing hdr.length and hdr.count. I tried it with gcc 4.8 and clang 3.5 and it works correctly.

#include <iostream>
#include <fstream>

#pragma pack(push, r1, 1)
struct Header {
    char id[15];
    int length;
    int count;
};
#pragma pack(pop, r1)

int main() {
  Header h = {"alalalala", 5, 10};

  std::fstream fh;
  fh.open("test.txt", std::fstream::out | std::fstream::binary);
  fh.write((char*)&h, sizeof(Header));
  fh.close();

  fh.open("test.txt", std::fstream::in | std::fstream::binary);

  fh.read((char*)&h.id, sizeof(h.id));
  fh.read((char*)&h.length, sizeof(h.length));
  fh.read((char*)&h.count, sizeof(h.count));

  fh.close();

  std::cout << h.id << " " << h.length << " " << h.count << std::endl;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reading binary data into struct with ifstream

From Dev

Reading a struct in binary with ifstream read()

From Dev

C++ reading binary data to struct

From Dev

Reading Binary Files w/ ifstream

From Dev

Reading binary data written using Python struct in C

From Dev

Fstream not reading a complete struct from binary data (C++)

From Dev

Why is ifstream failing when reading mixed data?

From Dev

C++: Improving ifstream binary file reading speed

From Dev

Reading binary unsigned short from a file in C++ using ifstream

From Dev

Python Struct for Binary Data

From Dev

Reading binary data issues

From Dev

Reading binary data in Java

From Dev

Binary data reading

From Dev

C - Reading from binary file into any struct

From Dev

Reading data from a file and into a struct

From Dev

Reading data from a file and into a struct

From Dev

Reading string into struct data in C

From Dev

Reading binary data with fstream method

From Dev

Reading alternating binary data into Matlab

From Dev

Reading Binary Data Using Python

From Dev

std::vector<unsigned char> remains empty after reading binary file using std::ifstream

From Dev

std::vector<unsigned char> remains empty after reading binary file using std::ifstream

From Dev

ifstream binary file wrong

From Dev

Reading data from file into a struct (C)

From Dev

Reading struct data from memory in C?

From Dev

Reading data from file to struct using fread

From Dev

insert a struct into a vector as binary data for network transmission

From Dev

Reading a raw binary data in python and converting it into ascii

From Dev

reading and converting binary raw data with R

Related Related

HotTag

Archive