如何将动态分配的多维结构数组写入文件?

时间:2012-05-02 12:48:19

标签: c++ fwrite

结构

struct str
{
  int a;
  char b[20];
  int c;
}

声明

struct str ** str_array=0;

分配和初始化(使用)

str_array= new struct str*[100];
for(int i=0;i<100;++i)
   str_array[i]=new struct str[1000];

for(int i=0;i<100;++i)
   for(int j=0;j<1000;++j;
      str_array[i][j].a=j;
       .....

现在我尝试编写像

这样的数组内容
for(int i=0;i<100;++i)
   for(int j=0;j<1000;++j;
     fwrite(str_array[i][j],sizeof(str),1,fname);

问题

  

没有从 str const void * 的合适转换

我打算用同样的方式阅读它。

1 个答案:

答案 0 :(得分:2)

变化:

 fwrite(str_array[i][j],sizeof(str),1,fname);

为:

 fwrite(&str_array[i][j],sizeof(str),1,fname);
相关问题