I am trying to read and write binary files. I create an array in ram, then write it to a binary file.
void escribirMatrizParcialmente(double ***A){
FILE *destino;
int i,j;
if((destino=fopen(ARCHIVOMATRIZ,"wb"))==NULL){
printf("No se pudo abrir el archivo.");
return;
}
for(i=0;i<FILAS;i++){
fwrite( &A[i][0],sizeof(double),COLUMNAS,destino);
//escribo desde el primer elemento de cada fila la cantidad de
//columnas
}
fclose(destino);
}
ARCHIVOMATRIZ
, FILAS
and COLUMNAS
they are constant.
Well this gives me default segmentation error.
There is no error creating the array as I display it on the screen before writing it, and if I use fprintf to write to a text file it works fine. With the code below I write the matrix correctly in a text file (I put it just to illustrate and to show that my problem is with fwrite ).
for(i=0;i<FILAS;i++){
for(j=0;j<COLUMNAS;j++){
fprintf(destino,"%.6lf ",(*A)[i][j]);
}
fprintf(destino,"%s","\n");
}
Well, I also have problems reading said array with fread
a code that is quite similar to the writing code. It doesn't give compilation errors or warnings, but when I show the array it doesn't show anything.
int i;
for(i=0;i<FILAS;i++){
fread( &A[i][0] ,sizeof(double), COLUMNAS, fuente );
}
I read all the rows from the array and store them in my array from the starting memory address of each row.
A
is a triple pointer, then in this statement:You are writing a memory location instead of a value.
You say that you work with a matrix, that is, with a two - dimensional array... this is modeled with a double array and, as I have mentioned, here you are using a triple pointer . Declaring a triple pointer in this case would only make sense if the function has to allocate memory for the array... or free it. in any other case, for clarity, it would be advisable to use a double pointer:
For the other problems you lack information so the rest of the answer will have to wait.