This program should call a function that opens an external file and browse it counting the rows of it, then it creates a pointer to a matrix and browses again the file but now saving its data on the matrix and returns that matrix, after that it will print the matrix.
#include <stdio.h>
#include <stdlib.h>
int tam;
float **carga_archivo(char *nombre_archivo);
int main()
{
int i,j;
char *nombre_archivo="Agua_Vapor.txt";
float **agua_vapor=carga_archivo(nombre_archivo);
for (i = 0; i < 6; i++)
{
for (j = 0; i < tam; i++)
printf("%f ", agua_vapor[i][j]);
printf("\n");
}
return 0;
}
float **carga_archivo(char *nombre_archivo)
{
int i=0;
float P[300][6];
FILE *archivo;
archivo=fopen(nombre_archivo,"r");
while(!feof(archivo))
{
i++;
fscanf(archivo,"%f\t%f\t%f\t%f\t%f\t%f\n",
&P[0][i],&P[1][i],&P[2][i],&P[3][i],&P[4][i],&P[5][i]);
//This part is just so the program can read the file line per line,
//else it would count character per character, doesn't really do anything
//(I didn't know the command or condition to do it other way
}
tam=i;
printf("%i",tam);
int filas = 6;
int columnas = tam;
float **M = (float **)malloc(filas*sizeof(float*));
for (i=0;i<filas;i++)
M[i] = (float*)malloc(columnas*sizeof(float));
for (i = 0; i < columnas; ++i)
fscanf(archivo,"%f\t%f\t%f\t%f\t%f\t%f\n",&M[0][i],&M[1][i],
&M[2][i],&M[3][i],&M[4][i],&M[5][i]);
fclose (archivo);
return M;
}
The problem here is that when the matrix should be printed the program crashes, I know the program does saves the data, since when I print it directly inside the function I does prints, so I think it might be ether the way I'm declaring the matrix or the function or the way I'm calling to the function.
Edit: The content of the file it calls is just a data set separated by tabulations.
0.06 36.16 23.739 2425.0 2567.4 8.3304
0.06 80.00 27.132 2487.3 2650.1 8.5804
0.06 120.00 30.219 2544.7 2726.0 8.7840
0.06 160.00 33.302 2602.7 2802.5 8.9693
0.06 200.00 36.383 2661.4 2879.7 9.1398
0.06 240.00 39.462 2721.0 2957.8 9.2982
0.06 280.00 42.540 2781.5 3036.8 9.4464
0.06 320.00 45.618 2843.0 3116.7 9.5859
0.06 360.00 48.696 2905.5 3197.7 9.7180
0.06 400.00 51.774 2969.0 3279.6 9.8435
(you can't note the tabulations but they are there)
Any correction is welcome.