2014-03-18 54 views
1

我有下面的代碼:`讀一個動態數組從一個文件功能

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

void load_from_file(int A[], int *n); 

int main() 
{ 
    int *A; 
    A = (int*)malloc(0); 
    int count = 0; 
    int i; 

    load_from_file(A, &count); 

    for(i = 0; i < count; i++) 
    { 
     printf("A[%d]=%d ", i, A[i]); 
     printf("\n"); 
     printf("&A[%d]=%p \n\n", i, &A[i]); 
    } 

    return 0; 
} 

void load_from_file(int A[], int *n) 
{ 
    FILE* fp; 
    int temp, i; 

    fp = fopen("data.txt", "r"); 
    if (fp == NULL) 
    { 
     printf("error!!"); 
     exit (1); 
    } 

    fscanf(fp, "%d", &temp); 
    *n = temp; 

    A = (int*) realloc(A, temp * sizeof(int)); 
    if (*A == NULL) 
    { 
     printf("error realloc!!"); 
     exit(1); 
    } 

    for(i = 0; i < temp; i++) 
    { 
     fscanf(fp, "%d", &A[i]); 
    } 

    for(i = 0; i < temp; i++) 
    { 
     printf("A[%d]=%d ", i, A[i]); 
     printf("\n"); 
     printf("&A[%d]=%p \n\n", i, &A[i]); 
    } 

    fclose(fp); 
} 

我想讀取文本文件到一個數組。 文件的第一行有數組元素的個數,第二行是數字元素。 我們通過realloc創建數組。 但是出了點問題..... 我有一些補丁,打印數組元素的地址。

但可惜的是,他們是不同的(不是所有的時間)的函數中,函數外,雖然數組通過引用傳遞(我想...)

請告訴我哪裏是錯誤,我該如何解決這個問題。

在此先感謝...

迪米特里

+0

你能給我一點data.txt文件,幾個樣本? – Claudiordgz

+1

您正在向函數傳遞一個指針,但您需要將指針傳遞給該函數的指針,以便該函數可以修改調用代碼中的值('main()'),或者從函數返回指針。 –

+0

for examle:第一行:3(表示3個元素),即n = 3,創建一個包含3個元素的數組。第二行:2 3 4.我們應該有A [0] = 2,A [1] = 3,A [2] = 4。這發生在函數內部,但功能外有完全不同的值和不同的地址... – user3434833

回答

1

電話:

int *A = load_from_file(&count); 

功能:

int *load_from_file(int *n) 
{ 
    FILE* fp; 
    int temp,i; 
    int *A = 0; 

    fp=fopen("data.txt","r"); 
    if (fp==NULL){fprintf(stderr, "error!!\n");exit (1);} 

    fscanf(fp,"%d",&temp); 
    *n=temp; 
    A = (int*) realloc(A,temp * sizeof(int)); 
    if (A == NULL) {fprintf(stderr, "error realloc!!\n");exit(1);} 
    for(i=0;i<temp;i++) 
     fscanf(fp, "%d",&A[i]); 
    for(i=0;i<temp;i++) 
    { 
     printf("A[%d]=%d ",i,A[i]); 
     printf("\n"); 
     printf("&A[%d]=%p \n\n",i,&A[i]); 
    } 
    fclose(fp); 
    return A; 
} 

這是一組變化的完全最小;代碼需要更多的工作。特別是,fscanf()調用應該被錯誤檢查。分配測試中有一個微妙而重要的變化:if (A == NULL)而不是原來的if (*A == NULL)


隨着有點較爲完整的錯誤檢查:

int *load_from_file(int *n) 
{ 
    FILE *fp; 
    int temp, i; 
    int *A = 0; 
    const char file[] = "data.txt"; 

    fp = fopen(file, "r"); 
    if (fp == NULL) 
    { 
     fprintf(stderr, "Failed to open file %s for reading\n", file); 
     exit(1); 
    } 

    if (fscanf(fp, "%d", &temp) != 1) 
    { 
     fprintf(stderr, "Failed to read number of entries\n"); 
     exit(1); 
    } 
    *n = temp; 
    A = (int *) realloc(A, temp * sizeof(int)); 
    if (A == NULL) 
    { 
     fprintf(stderr, "Failed to reallocate %zu bytes of memory\n", 
       temp * sizeof(int)); 
     exit(1); 
    } 
    for (i = 0; i < temp; i++) 
    { 
     if (fscanf(fp, "%d", &A[i]) != 1) 
     { 
      fprintf(stderr, "Failed to read entry number %d\n", i); 
      exit(1); 
     } 
    } 
    for (i = 0; i < temp; i++) 
     printf("A[%d]=%d: &A[%d]=%p\n", i, A[i], i, &A[i]); 
    fclose(fp); 
    return A; 
} 

我有,我用這將錯誤從4線處理降低到每1行出現錯誤的錯誤報告功能庫。我強烈建議你自己創建和使用這樣一個庫。

+0

非常感謝我的朋友!所以你說,寫一個像void readFromFile(int ** A,int * next)這樣的函數是不可能的。 ?或者它會很容易出錯? – user3434833

+0

有可能使用接口'void readFromFile(int ** A,int * next);'但我通常更願意返回指針傳入指針的指針。在實踐中,它沒有什麼重大區別。你可能會考慮'int readFromFile(char const * filename,int ** A,int * next);'它將文件名傳遞給函數並返回一個狀態(OK爲0,其他值表示失敗),所以你可以測試成功或失敗,而不是像目前的代碼一樣終止程序。您可以在我的代碼版本中返回空指針。很多的可能性... –

+0

謝謝!你很棒! – user3434833