2010-09-26 79 views
3

我想將多維數組保存到文件中。 A爲例結構:將結構保存到文件

struct StructSub { 
    unsigned short id; 
}; 
struct MyStruct { 
    struct StructSub sub[3]; 
}; 

// Use the struct 
struct MyStruct main; 
int i = 0; 
while (i < 3) { 
    main.sub[i].id = i; 
    i++; 
} 

對於這個例子,我想將數據文件保存在這個格式(普通文本):

MyStruct main { 
    StructSub sub[0] { 
     id = 0; 
    } 
    StructSub sub[1] { 
     id = 1; 
    } 
    StructSub sub[2] { 
     id = 2; 
    } 
} 

什麼是最簡單的方法是什麼?

回答

2

我猜的東西像這樣更符合你的要求。它不像它可能的那樣簡潔,但它非常簡單,可以很容易地擴展到適應其他結構。

void WriteIndent(FILE* file, int indent) { 
    int i = 0; 
    while (i < indent) { 
     fprintf(file, "\t"); 
     ++i; 
    } 
} 


void WriteStructSub(FILE* file, StructSub* s, char* id, int indent) { 

    WriteIndent(file, indent); 
    fprintf(file, "StructSub %s {\n", id); 

    WriteIndent(file, indent + 1); 
    fprintf(file, "id = %i;\n", s->id); 

    WriteIndent(file, indent); 
    fprintf(file, "}\n"); 

} 


void WriteMyStruct(FILE* file, MyStruct* s, char* id, int indent) { 

    WriteIndent(file, indent); 
    fprintf(file, "MyStruct %s {\n", id); 
    int i = 0; 

    while (i < 3) { 

     char name[7]; 
     sprintf(name, "sub[%i]", i); 

     WriteStructSub(file, &s->sub[i], name, indent + 1); 
     ++i; 

    } 

    WriteIndent(file, indent); 
    fprintf(file, "}\n"); 

} 


int main(int argc, char** argv) { 

    MyStruct s; 
    int i = 0; 
    while (i < 3) { 
     s.sub[i].id = i; 
     ++i; 
    } 

    FILE* output = fopen("data.out", "w"); 
    WriteMyStruct(output, &s, "main", 0); 
    fclose(output); 

} 
+0

謝謝,這有幫助! – Midas 2010-09-27 17:27:39

1

你可以使用基本的文件,只要確保你寫在二進制

FILE * pFile; 
pFile = fopen("structs.bin","wb"); 
if (pFile!=NULL) { 
    frwite(main, 1, sizeof(struct MyStruct), pFile); 
    fclose (pFile); 
} 

如果你這樣做,雖然這種方式,它不是最便攜的平臺,因爲是字節順序考慮。

+0

+1提到endian問題。 – 2010-09-26 15:48:01

3

試試這個

struct StructSub { 
    unsigned short id; 
}; 
struct MyStruct { 
    struct StructSub sub[10]; 
}; 

// Use the struct 
struct MyStruct main; 
int i = 0; 
while (i < 10) { 
    main.sub[i].id = i; 
} 

寫入文件

FILE* output; 
output = fopen("Data.dat", "wb"); 
fwrite(&main, sizeof(main), 1, output); 
fclose(output); 

讀取文件

struct Data data; 
FILE* input; 
input = fopen("Data.dat", "rb"); 
fread(&main, sizeof(main), 1, input); 
// you got the data from the file! 
fclose(input); 

這些鏈接支持什麼上面的代碼是所有關於 - http://c-faq.com/struct/io.html

fwrite(&somestruct, sizeof somestruct, 1, fp); 
+3

您是否認真地在**'C' **程序中建議稱爲'main'的結構實例?! – 2010-09-26 11:08:00

+1

雅,因爲他想保存爲文件,它的工作原理 – 2010-09-26 11:18:13

+2

這絕對不會輸出在問題要求的文件格式。 – 2010-09-26 11:22:10

6

請記住,將原始結構體保存到像這樣的文件根本不是可移植的。編譯器可能會將填充添加到結構中(更改sizeof(your_struct)),endianness可能會不同,等等。但是,如果這不是問題,那麼fwrite()可以正常工作。

請記住,如果您的結構體包含任何指針,您希望寫入指針指向的數據,而不是指針本身的值。

從對象的名稱
1

除了能main,這可能會導致你的任何數量的奇怪的問題:剛蠻力吧 - 有沒有更好的辦法:)

/* pseudo code */ 
write struct header 
foreach element 
    write element header 
    write element value(s) 
    write element footer 
endfor 
write struct footer