2012-03-14 114 views
2

下面是定義的結構。從文件讀取結構內的結構

typedef struct{ 
    int a; 
    char b; 
}X; 
typedef struct{ 
    X m; 
    int c; 
    char d; 
}B; 
B n,q; 
n.m.a = 12; 
n.m.b = 'a'; 
n.c = 13; 
n.d = 'b'; 

我在文件中做了以下結構的fwrite。文件打開如下。

fp = fopen("D://tests//t11test.txt","wb"); 
fwrite(&n, sizeof(B), 1, fp); 

fwrite成功,我檢查了與fp對應的文件內容。 但是,當我關閉並重新打開文件後在同一文件上執行fread時,我無法讀取子結構m的內容。該fread是

fp = fopen("D://tests//t11test.txt","rb"); 
fread(&q, sizeof(B), 1,fp); 

我在哪裏出錯了?

+0

你是什麼意思「不能讀」? – 2012-03-14 09:49:00

+0

我猜你已經用一種破壞原始文件內容的模式重新打開文件。你可以發佈一個複製程序嗎? – sarnold 2012-03-14 09:49:55

+0

@ Karoly值** int c **和** char d **正確讀取。子結構X **的**成員沒有正確閱讀。 @sarnold我以「wb」模式寫入文件,並在「rb」中重新打開以讀取結構。其實我有兩個程序。一個將結構寫入文件,另一個讀取結果。 – 2012-03-14 09:52:36

回答

1

我看不出有什麼問題,但是,FWIW,這工作:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <errno.h> 
#include <assert.h> 

typedef struct{ int a; char b; }X; 
typedef struct{ X m; int c; char d; }B; 

void print_B(B* a_b) 
{ 
    printf("{ { %d, %c }, %d, %c }", 
      a_b->m.a, 
      a_b->m.b, 
      a_b->c, 
      a_b->d); 
} 

int main() 
{ 
    /* Write structure. */ 
    { 
     FILE* fp; 
     B n; 
     n.m.a = 12; 
     n.m.b = 'a'; 
     n.c = 13; 
     n.d = 'b'; 
     fp = fopen("x.dat", "wb"); 
     assert(0 != fp); 
     if (1 != fwrite(&n, sizeof(B), 1, fp)) 
     { 
      fprintf(stderr, "Failed to fwrite(): %s\n", strerror(errno)); 
      return 1; 
     } 
     fclose(fp); 

     printf("wrote: "); 
     print_B(&n); 
     printf("\n"); 
    } 

    /* Read structure. */ 
    { 
     FILE* fp; 
     B q; 
     fp = fopen("x.dat", "rb"); 
     assert(0 != fp); 
     if (1 != fread(&q, sizeof(B), 1, fp)) 
     { 
      fprintf(stderr, "Failed to fread(): %s\n", strerror(errno)); 
      return 1; 
     } 
     fclose(fp); 

     printf("read : "); 
     print_B(&q); 
     printf("\n"); 
    } 

    return 0; 
} 

輸出:

wrote: { { 12, a }, 13, b } 
read : { { 12, a }, 13, b } 
+0

感謝您的回答。它的工作..我會讓你知道我的問題。 – 2012-03-14 10:25:41

+0

沒問題,有興趣找出問題所在。 – hmjd 2012-03-14 10:26:40

1

我想你搞砸了別的東西,這個工程:

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

typedef struct{ 
    int a; 
    char b; 
} X; 
typedef struct{ 
    X m; 
    int c; 
    char d; 
} B; 

int main() { 
    FILE *fd; 
    B n, q; 

    n.m.a = 12; 
    n.m.b = 'a'; 
    n.c = 13; 
    n.d = 'b'; 

    if((fd = fopen("test.dat", "wb")) == NULL) { 
     fprintf(stderr, "Error opening file: %s\n", strerror(errno)); 
     exit(-1); 
    } 

    fwrite(&n, sizeof(n), 1, fd); 
    fclose(fd); 

    if((fd = fopen("test.dat", "rb")) == NULL) { 
     fprintf(stderr, "Error opening file: %s\n", strerror(errno)); 
     exit(-1); 
    } 

    fread(&q, sizeof(q), 1, fd); 
    fclose(fd); 

    printf(
     "n.m.a: %d, q.m.a: %d; n.m.b: %c, q.m.b: %c; n.c: %d, q.c: %d; n.d: %c, q.d: %c\n", 
     n.m.a, q.m.a, n.m.b, q.m.b, n.c, q.c, n.d, q.d 
    ); 

    return 0; 
} 

輸出:

n.m.a: 12, q.m.a: 12; n.m.b: a, q.m.b: a; n.c: 13, q.c: 13; n.d: b, q.d: b