1

I want to write an integer in a binary file with Java (Android) and then read it with a C++ code. My code in Java is:

byte [] mybuffer = ByteBuffer.allocateDirect(4).putInt(1000).array;
out.write(mybuffer, 0, 4); // out is FileOutputStream

The reader in C++

std::ifstream fileToRead;
fileToRead.open("myFile", std::ios::binary);
if (!fileToRead.is_open()){
    std::cout << "[ERROR] Can't open file" << std::endl;
    exit(-1);
}
int  * myInt = new int;
fileToRead.read((char*)&myInt[0], 4);
std::cout << " The integer is " << myInt[0] <<  std::endl;

But I get values which doesnt make sense.

Thanks

output Java:

buffer[0] = 0
buffer[1] = 0
buffer[2] = 3
buffer[3] = -24

output c++:

The integer is -402456576
4
  • you should add your weird outputs to your question :) (also there's no actual "question" in you question) Commented Aug 7, 2015 at 12:50
  • 1
    The actual reading-part of the C++ code looks legit, but it certainly got the feel of a Java coder. No need to dynamically allocate that myInt variable, the address-of operator & is enough: int myInt; fileToRead.read(reinterpret_cast<char*>(&myInt), sizeof(myInt)); Commented Aug 7, 2015 at 12:51
  • It'd be useful to see sample input and outputs, especially printed byte-by-byte. Commented Aug 7, 2015 at 12:52
  • In the Android part I get: mybuffer[0] = 0 mybuffer[1] = 0 mybuffer[2] = 3 mybuffer[3] = -24 and in the C++ part: myInt = -402456576 I removed the new of the int as well Commented Aug 7, 2015 at 13:00

2 Answers 2

4

You may encounter a byte-order problem:

#include <cstdint>
#include <fstream>
#include <iostream>
// For ntohl with Linux (Windows has similar):
#include <arpa/inet.h>

int main()
{
    // You can use the constructor to open the file:
    std::ifstream fileToRead("myFile", std::ios::binary);
    // Just check the general state of the stream:
    if(!fileToRead){
        std::cout << "[ERROR] Can't open file" << std::endl;
        // Please do not use exit to terminate a program:
        return -1;
    }
    // No need to allocate an integer. Also be specific about the size:
    int32_t myInt;
    // There might be byte order issues, here (Java is big-endian):
    fileToRead.read((char*)&myInt, sizeof(int32_t));
    // To fix it convert the integer from network byte order to host byte order:
    myInt = ntohl(myInt);
    std::cout << " The integer is " << myInt <<  std::endl;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes it works thanks ! Will this method works with a big array of float too ? Thanks
1

For good order, as java uses per default BIG_ENDIAN byte order:

byte[] mybuffer = ByteBuffer.allocateDirect(4)
                  .order(Order.LITTLE_ENDIAN).putInt(1000).array();

That is the order on Intel processor's memory architecture.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.