-3

How do you convert this java code of reading in a file to be in C++. Just trying to read a file in and then print the heading of the file. I can do it in Java but would like to have it in C++

        f = new DataInputStream(new FileInputStream(directory + fileName));

        int magic_num = f.readInt();
        if (magic_num != 0xBBBBBAAD) {
            f.close();
            return(null);
        }
        int version = f.readInt();
        fi.height = f.readInt();
        fi.width = f.readInt();
        int type = f.readInt();
        fi.nImages = f.readInt();
        fi.offset = f.readInt();
        int flags = f.readInt();

        System.out.println(">>ARF header: "+magic_num+" "+version+" "+fi.width+" "+fi.height+" "+type+" "+fi.nImages+" "+fi.offset);

This what I have go so far, but can not get it to print the correct data.

#include <iostream>
#include <fstream>
#include <inttypes.h>
using namespace std;


int main () {
  streampos size;
  char * memblock;

  ifstream file ("/home/arf/0001.arf", ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();


    printf("%02x", memblock[0]);


    delete[] memblock;
  }
  else cout << "Unable to open file";
  return 0;
}
Tom
  • 1
  • 1
  • 2
    stack overflow isn't a write-my-code-for-me site – user253751 Jun 02 '22 at 17:21
  • What happens when you run the code? What did you expect to happen instead? Any errors? See also [ask]. Can you include a sample file here? – Robert Jun 02 '22 at 17:32
  • I just get ffffffbb instead of 0xBBBBBAAD like in the Java code. – Tom Jun 02 '22 at 17:36
  • basically all I want is the C++ code to work exactly like the java code, so that I can replace that portion. – Tom Jun 02 '22 at 17:38
  • 1
    A better way to read the file: https://stackoverflow.com/a/36659103/4581301 Using `vector` and iterators turns it into a leak-proof 2-liner. – user4581301 Jun 02 '22 at 17:38
  • The size of a `char` is 1 byte. The size of an `int` is 4 bytes. You are going to need to print at least 3 more bytes to see the magic number. – debido Jun 02 '22 at 17:39
  • 1
    Side note: [`int` isn't always 4 bytes](https://en.cppreference.com/w/cpp/language/types). Only the minimum, 2 bytes, is specified by the C++ Standard. The maximum size floats; `int` can be no larger than `long` and `long` can be no larger than `long long`, but nothing says they can't all be the same size and 4096 bits. When you need an exact size, you have to ask for it with a [fixed width integer](https://en.cppreference.com/w/cpp/types/integer). In this case `uint32_t` is probably the best fit. – user4581301 Jun 02 '22 at 17:44
  • 1
    You might also have to watch out for the byte order. If I remember correctly, Java defaults to big endian. C++ defaults to the hardware's native endian, which these days tends to be little endian. You'll probably have to flip the order around. The almost ubiquitous `htonl` and `ntohl` functions may be able to help you here. – user4581301 Jun 02 '22 at 17:47
  • 1
    Here's a great Q&A that should get you going: [Read 32-bit integer from binary file in C++?](https://stackoverflow.com/questions/32066027/read-32-bit-integer-from-binary-file-in-c) The asker's doing the wrong thing, but the top-voted answer nails what you need in order to read the magic number. The rest should fall together (or allow you to write a more focused question) from understanding the answer. – user4581301 Jun 02 '22 at 17:53
  • The big take-away: Write your own `readInt` function that takes the stream by reference, reads the stream as per Paulsm's answer to the question linked above, performs whatever transformations are required to sort out endian and returns an `int`. Then call this function as necessary. This is a rare case where writing the C++ code the same way as the Java code is the right solution--you just have to provide the `readX` scaffolding functions to get the data from the file.. – user4581301 Jun 02 '22 at 17:59

0 Answers0