I have a binary file written in C and I want to read it in java. I wrote the file in C like this :
void Log(TCHAR *name,int age){
write_int(file, 2012);
write_int(file, 4);
write_int(file, 16);
write_int(file, 12);
write_int(file, 58);
write_int(file, 50);
fwrite(&name, sizeof(name), 1, file);
fwrite(&age, sizeof(age), 1, file);
fflush(processusFile);
}
In Java I use the BinaryFile class http://www.heatonresearch.com/code/22/BinaryFile.java and I do this :
RandomAccessFile f = new RandomAccessFile("myfile.dat", "r");
BinaryFile binaryFile = new BinaryFile(f);
ArrayList<String> text = new ArrayList<>();
while (true) {
try {
Calendar calendar = new GregorianCalendar();
int year = (int) binaryFile.readDWord();
int month = (int) binaryFile.readDWord();
int date = (int) binaryFile.readDWord();
int hourOfDay = (int) binaryFile.readDWord();
int minute = (int) binaryFile.readDWord();
int second = (int) binaryFile.readDWord();
calendar.set(year, month, date, hourOfDay, minute, second);
System.out.println(binaryFile.readFixedString(64));
catch (Exception e) {
break;
}
}
This is not working for char* but for int it works. How I can write Strings?
This is because
sizeof(name)is the size of acharpointer on your system, not the length of the string. You need to write out the length separately from the string, too.