Search code examples
carraysreadfile

Segmentation fault(core dumped ) error while reading a python generated binary array in C


I am trying to load a 2D array created by numpy and read the elements in C, but I get Segmentation fault(core dumped ) error while running it. The code goes by the lines of


#include <stdio.h>
#include <string.h>
int main(){
    char *file;
    FILE *input;
    int N1, N2, ii, jj;
    float element;
    strcpy(file, "/home/caesar/Desktop/test.bin");
    input = fopen(file, "rb");
    fread(&N1, sizeof(int), 1, input);
    fread(&N2, sizeof(int), 1, input);
     float memoryarray[N1][N2];
    for(ii= 0; ii<N1; ii++){
        for(jj=0; jj<N2; jj++){
            fread(&element, sizeof(float), 1, input);
            memoryarray[ii][jj]= element; 
        }
    }
    printf("%f", memoryarray[2][3]);
    fclose(input);
    return 0;
} 

This is the starting for a task where I will have to read elements from matrices of the form 400*400*400 or so. The idea is to read all elements from the file and store it in memory and then read from memory index wise when necessary, for example, here i am trying to access and print the element in the second row third column.

P.S: I am quite new to pointers.

Dear all, I tried the methods you said., here is the modified version of the code, the segmentation fault error is gone but the output is either all zeros, or is just plain garbage values. I ran the executable three times and the outputs I got were Output1: -0.000000 Output 2: 0.000000 Output 3 : -97341413674450944.000000

My array contains integers btw

Here is the modified version of the code

#include <stdio.h>
#include <string.h>
void main(){
    const char file[] ="/home/caesar/Desktop/test.bin";
    FILE *input;
    int N1, N2, ii, jj;
    float element;
    //strcpy(file, "/home/caesar/Desktop/test.bin");
    input = fopen(file, "r");
    fread(&N1, sizeof(int), 1, input);
    fread(&N2, sizeof(int), 1, input);
     float memoryarray[N1][N2];
    for(ii= 0; ii<N1; ii++){
        for(jj=0; jj<N2; jj++){
            fread(&element, sizeof(float), 1, input);
            memoryarray[ii][jj]= element; 
        }
    }
    printf("%f", memoryarray[1][2]);
    fclose(input);




Also here is the hex dump of the file that i am trying to open. Some of you asked me to verify whether fopen() is working or not, i checked, it is working.

00000000  00 00 40 40 00 00 40 40  01 00 00 00 00 00 00 00  |..@@..@@........|
00000010  02 00 00 00 00 00 00 00  03 00 00 00 00 00 00 00  |................|
*
00000030  04 00 00 00 00 00 00 00  04 00 00 00 00 00 00 00  |................|
00000040  05 00 00 00 00 00 00 00  06 00 00 00 00 00 00 00  |................|
00000050


So here is my problem in brief. I have multidimensional arrays of double precision floats written to a file using python. I want to take those files and access the elements whenever necessary by using the index of the elements to get the values. Any C code to do so would solve my problem.

Here is the python code i am using to write the file

with open('/home/caesar/Desktop/test.bin', 'wb') as myfile:
    N= np.zeros(2, dtype= np.float32, order= "C")
    N[0]= 3
    N[1]= 3
    a= [[1,2,3],[2,3,4], [4,5,6]]
    N.astype(np.float32).tofile(myfile)
    b= np.asarray(a)
    b.tofile(myfile)


Solution

  • strcpy(file, "/home/caesar/Desktop/test.bin");

    This writes to a garbage memory address. You should either declare file as an array of suitable size, like this:

    char file[100];
    

    or

    initialize the char pointer directly with the path like this (and get rid of the strcpy):

    const char *file = "/home/caesar/Desktop/test.bin";
    

    or the best, as per common consensus (refer comments):

    fopen("/home/caesar/Desktop/test.bin", "rb");