How do I read a structure containing dynamic arrays from a file?

 

I have this code to a structure containing dynamic arrays from a file.  How do I read  the array of struture? Thank you

struct test
{
   int               zero;
   int               one[];
   double            two[];

   void write(int h){
      FileWriteInteger(h,zero);
      int size = ArraySize(one);
      FileWriteInteger(h, size);
      for( int i = 0; i < size; i++ )
         FileWriteInteger(h, one[i]);   
      size = ArraySize(two);
      FileWriteInteger(h, size);
      for( int i = 0; i < size; i++ )
         FileWriteDouble(h, two[i]);   
   }
};

test guy;


void OnStart()
{
   int handle = FileOpen("test.bin", FILE_BIN | FILE_WRITE);
   guy.write(handle);
   FileClose(handle);
   
}
 
Sunfire:

I have this code to a structure containing dynamic arrays from a file.  How do I read  the array of struture? Thank you

Hello , you do the same for the read section 

void read(int h){
zero=(int)FileReadInteger(h);
int total=(int)FileReadInteger(h);
if(total>0){
ArrayResize(one,total,0);
for(int i=0;i<total;i++){one[i]=(int)FileReadInteger(h);}
}else{ArrayFree(one);}
total=(int)FileReadInteger(h);
if(total>0){
ArrayResize(two,total,0);
for(int i=0;i<total;i++){two[i]=(int)FileReadInteger(h);}
}else{ArrayFree(two);}
}
 
Lorentzos Roussos #:

Hello , you do the same for the read section 

Thank you

 
Sunfire #:

Thank you

Anytime


 
Lorentzos Roussos #:

Anytime


This way OnStart?


void OnStart()
{
   
   test gal;
   int handle = FileOpen("test.bin", FILE_BIN | FILE_WRITE);
   gal.read(handle);
   FileClose(handle);
}
 
Sunfire #:

This way OnStart?

Yes but FILE_READ instead of FILE_WRITE

 
Lorentzos Roussos #:

Yes but FILE_READ instead of FILE_WRITE

Oh ya, thank. Look like you have some neat Products. I will take a look tomorrow.

 
Sunfire #:

Oh ya, thank. Look like you have some neat Products. I will take a look tomorrow.

Thank you 


 
Lorentzos Roussos #:

Thank you 



One last request of help, how to open read and write?

struct test
{
   int               zero;
   
string             one;
   double            two[];
   string            three;
}

test guy[];


into a bin file.

I have requested to add you as friend, interested in your product. 
 
Sunfire #:


One last request of help, how to open read and write?


into a bin file.

I have requested to add you as friend, interested in your product. 

For strings i use a custom class , its handy because i can call reset , load , save and i dont have to care.

Other than that , you can turn the string to a char array , get the length of the array , store the total and then each char as you do with the other arrays.

Im attaching the custom class in case you want to use it .

Files:
Reason: