Read from binary in c++

Viewed 59

I am trying read float array from bin file in C++. But I created this bin file with java, and I need this float array read in C++.

Write to binary file from java.

public static void main(String args[]){
    String filename = "out.bin";
    try{
        FileOutputStream fileOs = new FileOutputStream(filename);
        ObjectOutputStream os = new ObjectOutputStream(fileOs);
        os.writeFloat((float) 1.257);

    }catch(FileNotFoundException e){
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }

    System.out.println("Write Done");

}

and read in C++

  std::ifstream fileData("out.bin", std::ios::in | std::ios::binary);

  if(!fileData) {
    std::cout << "Cannot open file.\n";       
  }
  float getdata;
  fileData.read((char *) &getdata, sizeof(getdata));
  fileData.close();

  std::cout << "Data: "<< getdata << std::endl; 
  }

I write 1.257 but I read in C++ Data: 6.06218e-36.

How can I read correct way and if I have float array?

0 Answers
Related