In a Mac (thus, 64 bit, little endian), I want to read a binary data file, created in a ~1989 SGI (Irix 3.x, 680x0 Motorola CPU, thus, 32 bit, big endian).
Data in that file was written using the C code:
fwrite(&buf_float,FLOATBYTEBUFSIZE,1,fp_outFile);
In the Mac, which C code should I use to read that data?
In Xcode, I tried the code below, which compiles but fails at run time:
#import <CoreFoundation/CoreFoundation.h>
CFSwappedFloat32 swappedFloat;
fread(&swappedFloat, sizeof(swappedFloat), 1, ifp); // Error: "Thread 1: EXC_BAD_ACCESS (code=1, address=0x68)"
f = CFConvertFloat32SwappedToHost(swappedFloat);
main.c (MinimalWorking Code):
#include <stdio.h>
#include <stdlib.h>
#import <CoreFoundation/CoreFoundation.h>
int main(int argc, const char * argv[]) {
FILE *ifp;
printf("\n Opening for reading binary file: %s...", argv[1]);
ifp = fopen(argv[1], "r");
printf("\n Reading 1st value float...");
CFSwappedFloat32 swappedFloat;
fread(&swappedFloat, sizeof(swappedFloat), 1, ifp); //Thread 1: EXC_BAD_ACCESS (code=1, address=0x68)
float f = CFConvertFloat32SwappedToHost(swappedFloat);
printf("\n Read value %f", f);
fclose(ifp);
printf("\n DONE.\n");
return 0;
}
Download test binary data file "inFile" (and, optionally, minimum Xcode project): https://drive.google.com/drive/folders/1OatRzDrPak2KNRH5ZhrpjP4S3Z9Ps3jY
Full stack trace:
Opening for reading binary file: inFile...
Reading 1st value float...(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x68)
frame #0: 0x00007fff6f3a5a5d libsystem_c.dylib`flockfile + 18
frame #1: 0x00007fff6f3a76ad libsystem_c.dylib`fread + 31
* frame #2: 0x0000000100003e3d MWE1`main(argc=2, argv=0x00007ffeefbff540) at main.c:35:5
frame #3: 0x00007fff6f31dcc9 libdyld.dylib`start + 1
(lldb)