0

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) 
sambaMan
  • 11
  • 2
  • 3
    Minimal verifiable code example should be posted in the question. Not as links. And certainly not as links that require a sign up and login to an external site. – kaylum Sep 01 '20 at 01:56
  • Any error messages should be included verbatim – erik258 Sep 01 '20 at 01:58
  • An MRE is just precisely relevant Source Code added in unformatted text form **within** the question. – A P Jo Sep 01 '20 at 04:29
  • What does your research concerning the format of the SGI's `float` reveal? Is it compatible with the Mac's `float`, after the endianness is handled? – the busybee Sep 01 '20 at 07:25
  • 1) @kaylum, A P Jo. I've just posted the minimum verifiable code in the body of the question . – sambaMan Sep 01 '20 at 13:51
  • 2) @kaylum, I posted the binary data file (apparently can't post to stackOveflow) in a link that non-Apple users can access: https://drive.google.com/drive/folders/1OatRzDrPak2KNRH5ZhrpjP4S3Z9Ps3jY – sambaMan Sep 01 '20 at 13:52
  • 3) @Daniel Farrell, I posted the error message in the question Verbatim, "as is" displayed by Xcode: "Thread 1: EXC_BAD_ACCESS (code=1, address=0x68)" – sambaMan Sep 01 '20 at 13:52
  • 4) @the busybee 5, I did not find specific info for that old SGI. It is my 1st time using Mac's Core Foundation. Am I using these libraries correctly? If yes, does anyone know of alternative libraries or functions that could work? – sambaMan Sep 01 '20 at 13:56
  • 5) Added Stack trace (from lldb bt). – sambaMan Sep 01 '20 at 14:17
  • Check the return value of `fopen` before attempting to use the resulting file pointer. – kaylum Sep 01 '20 at 23:31

1 Answers1

1

The file pointer was null (thanks @kaylum for his comment). That was because inFile's permissions were wrong. I fixed them with "chmod -R 777 ./". The program now runs in the Terminal (compiled with gcc), and reads float values. Whether they are correct values or not depend on further 3D visualization work over the next few days. Again many thanks to all who commented!

sambaMan
  • 11
  • 2