Infinite Loop when reading specified number of bytes from binary file
Infinite Loop when reading specified number of bytes from binary file
I have a binary file that starts something like this (in hex):
FFFF7FFF FFEFFFFF FFFFFFFF 0000454C 0000AA0E 2D2D2D2D 00002892 425A6839 31415926 5359F7B8
FA590000 A4FFFFFF FFFFFFFF FFFFFFFF FFFFFDFF EBFFFFFF FFFF7FFF FFEFFFFF FFFFFFFF FFFFFFE0
00
I'm trying to read in the header with C++. I've got code that looks like this:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
typedef struct {
char tp[9];
char xt[3];
uint32_t jdt;
uint32_t jtm;
char nm[4];
} Header_1;
int main(){
Header_1 test;
ifstream infile("./testbin", ios::in | ios::binary);
vector<unsigned char> head_bytes(24, 0);
infile.read((char*)&test, head_bytes.size());
vector<unsigned char> comp_record(12, 0);
infile.read((char*)&comp_record, comp_record.size());
cout <<(char)comp_record[0]<<endl;
}
I'm compiling with clang/g++ on macOS 10.13.4, g++ --version
output below:
g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Whenever I compile/run the code, I get one of two outcomes depending on whether or not I leave in the last cout line. If it is in there, the program crashes with a Segmentaion Fault 11 (on macOS this appears to be a stack size issue, my stack size is 8192
and I don't see a reason that indicates that increasing it will resolve my problem). If I remove the cout line, I get stuck in an infinite loop.
8192
Given that I give a specified number of bytes to read, I have no idea how I get stuck in a loop nor any idea how I run out of stack space.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.