2016-12-06 78 views
-2

該程序應該調用一個函數,該函數打開一個外部文件並瀏覽它,計算它的行​​數,然後創建一個指向矩陣的指針並再次瀏覽文件,但現在保存它在矩陣上的數據並返回該矩陣,然後它將打印矩陣。保存並打印一個函數中的另一個文件的矩陣

#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

(你不能記下表格,但他們在那裏)

歡迎任何修正。

+1

你的指數'P'似乎周圍的錯誤的方式。或者其聲明。 – Evert

+1

你的代碼有很多問題。顯示'Agua_Vapor.txt'的內容樣本 – BLUEPIXY

+0

http://stackoverflow.com/q/5431941/3185968 – EOF

回答

0

修復此類

#include <stdio.h> 
#include <stdlib.h> 

#define COLS 6 
int tam; 

float **carga_archivo(const char *nombre_archivo); 

int main(void){ 
    int i,j; 
    const char *nombre_archivo= "Agua_Vapor.txt"; 
    float **agua_vapor = carga_archivo(nombre_archivo); 

    for (i = 0; i < tam; i++){ 
     for (j = 0; j < COLS; j++) 
      printf("%f ", agua_vapor[i][j]); 
     printf("\n"); 
    } 
    //deallocate agua_vapor 
    return 0; 
} 

float **carga_archivo(const char *nombre_archivo){ 
    float P[300][COLS]; 
    FILE *archivo = fopen(nombre_archivo, "r"); 
    int i= 0; 

    while(i < 300 && COLS==fscanf(archivo, "%f\t%f\t%f\t%f\t%f\t%f\n", &P[i][0],&P[i][1],&P[i][2],&P[i][3],&P[i][4],&P[i][5])){ 
     i++; 
    } 
    fclose(archivo); 
    tam=i; 
    //printf("%i",tam); 
    int rows = tam, columnas = COLS; 
    float **M = malloc(rows * sizeof(float*)); 
    for (i = 0; i < rows; i++){ 
     M[i] = malloc(columnas*sizeof(float)); 
     for(int j = 0; j < columnas; ++ j) 
      M[i][j] = P[i][j]; 
    } 
    return M; 
} 
+0

謝謝,那正是我所需要的,但是你能否解釋一下你在那裏做了些什麼或者我錯了什麼? –

+0

具體來說,你想問什麼?因爲有這麼多。 – BLUEPIXY

相關問題