試圖編寫一個函數,將我的結構數組寫入二進制文件。
我以爲我能夠一次將它作爲一個完整的單位拷貝過來,但它不適合我。二進制文件和寫入結構
我是否需要編寫每個單獨的子實體或者是否有辦法在一個大團隊中完成它?
{
void export_binary(char *data_base_name, student_record *ptr,int array_flag,unsigned int rec_cnt)
{
if (array_flag==-99)
{
printf("\n\nDatabase not loaded...\n\nPlease IMPORT or CREATE a new database.\n\n");
system("pause");
return;
}
int rec_counter;
FILE *pf;
pf=fopen(data_base_name,"wb");
if (!pf)
{
printf("*** FILE OPENING ERROR ***\n\n");
system("pause");
return ;
}
for (rec_counter=0; rec_counter <= rec_cnt; rec_counter++)
{
fwrite(&ptr[rec_counter], sizeof(student_record), 1, pf);
}
if ((fclose(pf))!=0)
{
printf("\n\n*** FILE Error - Closing file FAILED! ***\n\n");
system("pause");
return;
}
printf("\n\n*** Database SAVED ***");
system("pause");
return;
}
附加信息:我得到了我的記錄元素是一個文件,但垃圾的一切。我將結構指針從main傳遞給函數... student_record * ptr – 2012-03-13 09:09:31