2013-09-24 28 views
0

我需要編寫一個讀取各種文件並將信息存儲到數組中的程序。我將使用雙打執行矩陣乘法。關於文件的格式;第一行包含矩陣的大小。接下來的幾行是矩陣的行,其中每個元素由空格分隔。在不同寬度的文件中使用C中的fscanf

格式:

<number of rows> <number of columns> 
<double> <double> ... <double> 
<double> <double> ... <double> 
. 
. 
. 
<double> <double> ... <double> 

下面是幾個例子文件:

3 4 
1.20 2.10 4.30 2.10 
3.30 3.10 5.20 2.80 
1.10 0.60 4.70 4.90 

5 5 
1.20 2.10 4.30 2.10 6.70 
3.30 3.10 5.20 2.80 3.20 
1.10 0.60 4.70 4.90 9.10 
3.30 3.10 5.20 2.80 3.20 
1.10 0.60 4.70 4.90 7.10 

目前我的代碼如下:

float** readFile(char* fp) 
{ 
    float** matrix = (float**)malloc(M*N*sizeof(float)); 

    fp = fopen(fp, "r"); 

    if (fp == NULL) 
    { 
     fprintf(stderr, "Can't open the file\n"); 
     exit(1) 
    } 

    int i = 0; 
    int m, n; 
    fscanf(fp, "%d %d", m, n); 
    while (fscanf(fp, ""); 
    { 
     i++; 
    } 

    fclose(fp); 

    return matrix; 
} 

和我打電話這樣的功能:

float** A = readFile(argv[1]); 

顯然,這將不會在時刻的fscanf讀取文件的工作,因爲缺少論據。我如何使用fscanf將值讀入矩陣?

+0

讀取文件作爲字符串,分析該字符串多少空格有你現在將如何浮動(如果有的話)在線。 – SJuan76

回答

1

要簡單的事情,讓我們用一個維數組。這是你的工作代碼。

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

float* readFile(char* fp, int *m, int *n)/* return the dimension defined in file*/ 
{ 

    FILE *file = fopen(fp, "r"); 

    if (file == NULL) 
    { 
     fprintf(stderr, "Can't open the file\n"); 
       exit(1); 
    } 

    int i = 0, j = 0; 
    fscanf(file, "%d %d[^\n]\n", m, n); 
    float* matrix = (float*)malloc((*m)*(*n)*sizeof(float)); 

    float f; 
    for (i = 0; i < *m ; ++i) 
    for (j = 0; j < *n ; ++j) 
    { 
     fscanf(file, "%f", (matrix + i * (*n) + j)); 
    } 


    fclose(file); 

    return matrix; 
} 

int main() 
{ 
    int m, n, i, j; 
    float *a = readFile("a.dat", &m, &n); /* i named your data file a.dat*/ 

    for (i = 0; i < m ; ++i) 
     { 
       for (j = 0; j < n ; ++j) 
        printf("%f ", *(a + i * n + j)); 

       printf("\n"); 
     } 

     free(a); 
} 


    /* this is the output */ 
1.200000 2.100000 4.300000 2.100000 6.700000 
3.300000 3.100000 5.200000 2.800000 3.200000 
1.100000 0.600000 4.700000 4.900000 9.100000 
3.300000 3.100000 5.200000 2.800000 3.200000 
1.100000 0.600000 4.700000 4.900000 7.100000 
2

修改這個功能

float** readFile(char* file) 
{ 
    FILE *fp; 
    float** matrix = (float**)malloc(M*N*sizeof(float)); 

    fp = fopen(file, "r"); 

    if (fp == NULL) 
    { 
     fprintf(stderr, "Can't open the file\n"); 
     exit(1) 
    } 

    int i = 0; 
    int m, n; 
    fscanf(fp, "%d %d", m, n); 
    while (fgets(line,size(line),fp)!=NULL) //read file line by line 
    { 

使用strtok()到線分成令牌與分隔空間
使用strtof()將字符串轉換爲浮動

} 

    fclose(fp); 

    return matrix; 
} 
0

這裏是一個更版本,讀取數據爲動態分配的二維數組:

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

float** arralloc(int r, int c) { 
    int i; 
    float** arr = (float**) malloc(r*sizeof(float*)); 
    for (i = 0; i < r; i++) 
     arr[i] = (float*) malloc(c*sizeof(float)); 
    return arr; 
} 

int main(void) 
{ 
    int c,r,i,j; 
    FILE *file = fopen("file", "r"); 
    fscanf(file, "%d %d\n", &r, &c); 
    float** arr = arralloc(r,c); 
    for (i = 0; i < r; i++){ 
     for (j = 0; j < c; j++){ 
      fscanf(file, "%f", &arr[i][j]); 
      printf("%.2f ", arr[i][j]); 
     } 
     printf("\n"); 
    } 
    fclose(file); 
    free(arr); 
} 

enter image description here