简体   繁体   English

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

[英]How do I write a dynamically allocated multidimensional struct array to a file?

The struture 结构

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

declaration 宣言

struct str ** str_array=0;

allocation and initialization(use) 分配和初始化(使用)

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;
       .....

now I tried to write content of array like 现在我试着写出数组的内容

for(int i=0;i<100;++i)
   for(int j=0;j<1000;++j;
     fwrite(str_array[i][j],sizeof(str),1,fname);

Problem 问题

no suitable conversion from str to const void* exists 没有从strconst void *的适当转换

I intend to read it with same style. 我打算用同样的风格来阅读它。

Change: 更改:

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

to: 至:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM