該程序應該調用一個函數,該函數打開一個外部文件並瀏覽它,計算它的行數,然後創建一個指向矩陣的指針並再次瀏覽文件,但現在保存它在矩陣上的數據並返回該矩陣,然後它將打印矩陣。保存並打印一個函數中的另一個文件的矩陣
#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;
}
這裏的問題是,當基質應打印程序崩潰,我知道該程序不保存數據,因爲當我直接打印出來我不打印的功能裏面,所以我認爲這可能是乙醚我的方式聲明的矩陣或函數或我打電話給函數的方式。
編輯:它調用的文件內容只是一個由表格分隔的數據集。
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 3 9.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
(你不能記下表格,但他們在那裏)
歡迎任何修正。
你的指數'P'似乎周圍的錯誤的方式。或者其聲明。 – Evert
你的代碼有很多問題。顯示'Agua_Vapor.txt'的內容樣本 – BLUEPIXY
http://stackoverflow.com/q/5431941/3185968 – EOF