Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1200

 

Another question.

I have a structure.

struct Fann
{
   int      answer;
   double   v_main_up;
   double   v_add_up;
   double   v_main_dn;
   double   v_add_dn;
   double   data_1[];
   double   data_2[];
   double   data_3[];
};
Fann vfann;

I've filled it in and want to save it to a file.

FileWriteStruct(filehandle,vfann);

The compiler complains - 'vfann' - structures or classes containing objects are not allowed

I guess it doesn't like arrays. But how can I save this structure?
 
Сергей Таболин:

Another question.

I have a structure.

I've filled it in and want to save it to a file.

The compiler complains - 'vfann' - structures or classes containing objects are not allowed

I guess it doesn't like arrays. But how can I save this structure?

Yes, there should be no arrays.FileWriteStruct

...The structure must not contain strings,dynamic arrays,virtual functions and pointers to objects and functions.

Документация по MQL5: Файловые операции / FileWriteStruct
Документация по MQL5: Файловые операции / FileWriteStruct
  • www.mql5.com
//|                                          Demo_FileWiteStruct.mq5 | //|                        Copyright 2013, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //| Структура для хранения данных свечи                              |...
 
Сергей Таболин:

Another question.

I have a structure.

I've filled it in and want to save it to a file.

The compiler complains - 'vfann' - structures or classes containing objects are not allowed

I guess it doesn't like arrays. But how can I save this structure?
Create a structure with a structure instead of arrays. First, pack each array into its own structure. Then pack the resulting structure into the resulting structure. Unpack in reverse order.
 
Сергей Таболин:

I understand it doesn't like arrays. How do you save this structure?

translate it into text, save it element by element.

 
Artyom Trishkin:
Create a structure with a structure instead of arrays. First, each array should be packed into its own structure. Then pack the resulting structure into the resulting structure. Unpack in reverse order.
Igor Zakharov:

translate into text, save element by element.

Thank you.

How about this:

struct Fann
{
   int      answer;
   double   v_main_up;
   double   v_add_up;
   double   v_main_dn;
   double   v_add_dn;
};
Fann vfann;

   double   data_1[];
   double   data_2[];
   double   data_3[];
...........................
      FileWriteStruct(filehandle,vfann);
      FileWriteArray(filehandle,data_1);
      FileWriteArray(filehandle,data_2);
      FileWriteArray(filehandle,data_3);

FILE_BIN.

Then we can read it all into the structure and arrays in the same order?

What if there are several such records?

 
Сергей Таболин:

Thank you.

How about this:

FILE_BIN.

Then you can read it all into the structure and arrays in the same order?

And if there are several such records?

Yeah, I couldn't figure out that tip on the fly, either. If you can't do it at all, you can do it that way. But it's still better to figure out how to make a structure within a structure. It is enough to ask how to do it. At the same time, I will learn something else too...

 
Alexey Viktorov:

Yeah. I couldn't figure out that tip on the fly, either. If you can't do it at all, you can do it that way. But it's still better to figure out how to make a structure within a structure. It is enough to ask how to do it. At the same time, I will learn something else too.

Asking ))))

Tried to do it "my way".

The first structure and 3 arrays read fine (provided the arrays are not dynamic, otherwise the first array reads all the data written after the structure).

However, if there are more entries, it

   // Проверка
   double   rdata_1[6];
   double   rdata_2[6];
   double   rdata_3[6];
   
   filehandle = FileOpen(filename,FILE_READ|FILE_COMMON|FILE_BIN);
   if(filehandle != INVALID_HANDLE)
   {
      while(!FileIsEnding(filehandle))
      {
         FileReadStruct(filehandle,rfann);
         FileReadArray(filehandle,rdata_1);
         FileReadArray(filehandle,rdata_2);
         FileReadArray(filehandle,rdata_3);
         Print("++++++++++++++++++++++++++++++");
         Print("Структура");
         Print(rfann.v_main_up+"|"+rfann.v_main_dn+"|"+rfann.v_add_up+"|"+rfann.v_add_dn+"|"+rfann.answer);
         Print("Массивы");
         Print("--- 1");
         ArrayPrint(rdata_1);
         Print("--- 2");
         ArrayPrint(rdata_2);
         Print("--- 3");
         ArrayPrint(rdata_3);
      }
   }

it gives out the damn thing...

Структура
4.0|-1.0|2.8|-0.7|1
Массивы
--- 1
 1.00000  0.33225 -0.76202 -0.93263 -1.00000 -0.79174
--- 2
-0.14603  0.89562  0.91407  0.93450  0.89481  0.89829
--- 3
 0.89564  0.89217 -0.91174 -0.86623 -1.00000 -0.07680
++++++++++++++++++++++++++++++
Структура
1.839259944929932 e+277|-nan|5.295254096666168 e-315|3.0|-618173028
Массивы
--- 1
 2.10000 -2.00000 -1.40000 -0.85098 -1.00000 -0.79900
--- 2
-0.30490  0.47926  1.00000  0.70827  0.84163  0.84134
--- 3
 0.83925  0.84173  0.84760  0.84678  0.80368 -0.00063
++++++++++++++++++++++++++++++
Структура
9.384173261527221 e-276|5.298850499315376 e-315|2.955277867691187 e+299|-9.638234784517503 e-243|-1705145188
Массивы
--- 1
+0.00000 +0.00000  4.00000  2.80000  0.00000  0.00000
--- 2
 1.00000  0.22097 -0.37383 -0.73115 -0.81752 -1.00000
--- 3
-0.87753  0.46919  0.59706  0.76223  0.82610  0.84312
++++++++++++++++++++++++++++++
 
Artyom Trishkin:
Create a structure with a structure instead of arrays. First, each array should be packed into its own structure. Then pack the resulting structure into the resulting structure. Unpack in reverse order.

Artyom, can you give me an example? Given that the size of the arrays is dynamic.

 
Сергей Таболин:

Asking ))))

Tried to do it "my way".

The first structure and 3 arrays read fine (provided the arrays receivers are not dynamic, otherwise the first array reads all the data written after the structure).

However, if there are more entries, it

it gives out the damn thing...

You are trying to write 4 different files and only open one. Sorry.... there are no decent words.

 
Alexey Viktorov:

You are trying to write 4 different files and only open one. Sorry.... there are no decent words.

I write everything in one file. Writing, followed by reading, of several structures to one file goes well.