Seong Soo Lim Seong Soo Lim - 1 year ago 106
C++ Question

Different reading binary files in java / c++

My task is to read binary compressed files using JAVA.
there is a difference between reading as C++ and JAVA.

i don't know what the problem is when i read it with JAVA.
Please let me know the problem of JAVA code that i wrote.

please, help..

here is sample codes C++ and JAVA.
(no problem when reading with C++)

# in C++

ifstream file(path, ios_base::in | ios_base::binary);

while (!file.eof())
{ *)&j, sizeof(int));
if (file.eof()) break; *)&k, sizeof(int)); *)&result_c[j][k], sizeof(float)); *)&result_g[j][k], sizeof(float));

for (int l = 0; l < 6; l++)
{ *)&result_i[j][k][l], sizeof(float)); *)&result_t[j][k][l], sizeof(float));

# in Java

fin = new FileInputStream(new File(_path));
bin = new BufferedInputStream(fin);
din = new DataInputStream(bin);

boolean _eof = false;
while (!_eof) {

int _ny = 0, _nx = 0;
float _cResult = 0.0f, _gResult = 0.0f;
float[] _iResult = new float[6];
float[] _tResult = new float[6];

try {

_ny = din.readInt(); // ny
_nx = din.readInt(); // nx
_cResult = din.readFloat();
_gResult = din.readFloat();

for (int i = 0; i < 6; i++) {
_iResult[i] = din.readFloat();
_tResult[i] = din.readFloat();

} catch (EOFException eofe) {
_eof = true;

Answer Source

DataInputStream assumes the input is in network bye order. Your C++ code assumes it is in native byte order. If these are different, the results will be different too. DataInputStream also assumes that int and float are 32 bits, etc. See the Javadoc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download