简体   繁体   中英

Reading a binary file containing arrays in C or Mathematica

I've been sent a binary file, which I need to read part of. The file format was described to me as:

Line1: NLine

Line2: xarray1,yarray1

Line2: xarray2,yarray2

Line2: xarray3,yarray3

Line2: xarray4,yarray4

Line2: xarray5,yarray5

Line2: xarray6,yarray6

Line6: xarray7,yarray7

where NLine is an integer (499 to be specific) and each xarray and yarray is a float-array with length NLine. I actually only really care about xarray7 and yarray7, but I'm happy to read all of them into arrays for this purpose and just not use the others. Anyway, I just need to turn the arrays into ascii format so I can do something else with them, or even just convert them into arrays that I can deal with locally and print out. I'm happy to do this in either C or Mathematica.

Unfortunately I can't provide the data itself or an example of a similar file -- the above is all I know. If anyone can provide a bit of code that would let me get into my file (or tell me how the file couldn't possibly be in this exact format or something), that would be much appreciated.

Thanks!

Ok, just to get you going...

FILE* binFile = fopen("./path/filename.bin", "rb"); //open as binary 'b' for reading 'r'
char nextByte; // to store one byte in

// Loop through the whole file
while (!feof(binFile))
{
    // read one byte, with a size of 1 byte (could use sizeof(char) here), count of 1 from binFile, store in nextByte...
    fread(nextByte, 1, 1, binFile);

    // Print the byte
    printf("%02x ", nextByte);
}

// Close the file
fclose(binFile);

Once you do this it will print out the binary file in hex number (one byte per value). Then you can analyse the format. Note: I did not test this code... you will need to include the relevant c libraries too...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM