For example, in C, I have a structure:
typedef struct {
int number;
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
} CTRstruct;`
Then I write it to a file fwrite(&tr, 1, sizeof(tr), fp);(tr is its CTRstruct var, fp is the file pointer);
Then I need to read it using Java! I really don’t know how to read the structure from a file ... I tried to read it using ObjectInputStream(), the last idea is to read using RandomAccessFile(), but I also don’t know how ... ( readLong(), readDouble()also does not work, it works with the source, but does not read correct data). So, any idea how to read C struct from binary with Java?
If this is interesting, my version reads an integer (but it is ugly, and I don't know what to do with double):
Public class MyDataInputStream extends DataInputStream {
public MyDataInputStream(InputStream AIs) {
super(AIs);
}
public int readInt1() throws IOException{
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0));
}
(, int (8bytes), double native func).