Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1201

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

I write everything in one file. Writing, and then reading, several structures to the same file goes well.

But it reads the array from start to finish. How do you split it into 3 parts? How did you write a structure and manage to write an array to the same file? I'm self-taught, I don't know a lot, but the fact that they are different data formats, I somehow suspect.

I admit that you can manage to open the file for writing once and cram three arrays into it, but you can't just read it in parts like that.

 
Alexey Viktorov:

But it reads the array from start to finish. How do you split it into 3 parts? How do you write a structure and manage to write an array to the same file? I'm self-taught, don't know much, but the fact that they are different data formats is somehow suspect.

I admit that I can get away with opening a file for writing once and cram three arrays into it, but I can't read it that easily in parts.

I say, if you write to the same file only 1 structure and 3 arrays, then you can read them from there (assuming that to read the arrays you have to specify the size of each). But the trick of writing and then reading several such sequences didn't work for me. Perhaps I need to move the pointer, but it's already tense...

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

I told you, if you write only 1 structure and 3 arrays to one file, then you can read them from there later (provided that you have to specify the size of each of them to read the arrays). But the trick of writing and then reading several such sequences didn't work for me. Perhaps I need to move the pointer, but that's a bit cumbersome...

How is it tense? Not enough horsepower? And how can you be sure that the write was successful and how do you assume this if the read failed? As far as I know, you can't look at a .bin file with your eyes.

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

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

No, there will be no examples yet - I haven't done it myself, and when I do, I will describe it in my articles. And it will be not soon. The fact is that you can write only POD-structures into a file using FileWriteStruct(). That is simple structures that do not contain strings, dynamic arrays,virtual functions, as well as pointers to objects and functions.

And this means that you have to invent and test them. But I have written the direction. The implementation needs to be thought about.

 
Artyom Trishkin:

No, there won't be examples yet - I haven't done it myself yet, and when I do, I'll describe it in the articles. And it won't be soon. The fact is that only POD-structures can be written to file using FileWriteStruct(). That is, simple structures that do not contain strings, dynamic arrays, virtual functions, as well as pointers to objects and functions.

And this means that we have to invent and test it. But I have written the direction. The implementation should be thought over.

It turns out that no matter how you look at it, the task cannot be solved. After all, Sergei's example has three dynamic arrays inside. No matter how the structure is arranged in the structure, it will still remain a dynamic array.

But if you create an array of structures, the problem is solved by writing the array. FileWriteArray(). This has already been tested and the documentation clearly states about writing an array of structures.

 
Alexey Viktorov:

But it reads the array from start to finish. How do you split it into 3 parts? How do you write a structure and manage to write an array to the same file? I'm self-taught, I don't know a lot of things, but the fact that they are different data formats, somehow I suspect.

I admit, you can get away with it, opening a file for writing once and cram three arrays into it, but you won't be able to read it in parts like that.

In a binary file, you can write any structures, arrays, even single variables, all in one file.

BUT, when reading, you must read the data in the same order.

everything will work.

have not seen the file dump, when writing with FileWriteArray() , most likely, the array size will be written in the first bytes - but it's easier to make a test script, there are a dozen lines of code ;)

 
Alexey Viktorov:

It turns out that no matter how you look at it, the problem is unsolvable. After all, in Sergei's example there are three dynamic arrays inside. No matter how you build the structure in the structure, it will remain a dynamic array.

And if you create an array of structures, you will solve the problem by writing the array. FileWriteArray(). This has already been tested and it is clearly stated in the documentation about writing an array of structures.

An array of structures is stored, but... if there were strings, they can be packed into uchar array inside the structure, which is successfully done. But there is no way the array double can fit into the array uchar. Furthermore, it is dynamic. And saving the three arrays separately into files and then shoving them into the structure unpacked from the file... I haven't tried it. I haven't thought about it and don't know.

 
Igor Makanu:

you can write any structures, arrays, even individual variables into a binary file - all in the same file

BUT when reading, you have to read the data in the same order.

everything will work

have not seen the file dump when writing with FileWriteArray() , most likely, the array size will be written in the first bytes - but it's easier to make a test script, there are a dozen lines of code ;)

I don't know where you started reading about this problem, let me remind you just in case: you have to write a structure containing three dynamic arrays into a file. Then I had a question about the file not being read correctly.

Forum on Trading, Automated Trading Systems and Strategy Tests

FAQ from Beginners MQL5 MT5 MetaTrader 5

Sergey Tabolin, 2020.03.24 18:02

I'm asking ))))

I have tried to do it "my way".

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

However, if there are more entries, then

   // Проверка
   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 a hell of a lot...

Структура
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
++++++++++++++++++++++++++++++

So I'm thinking: how is it possible to read the structure and three arrays from one file, which you don't know how it was written.

That's why I said that I am self-taught and don't know a lot, but you can't read it in parts.

And I understand that there are options to add to .bin file everything, but to read it you need to know what is written in what order. And it is possible that not by means of mql.

 

I don't understand this at all...

      FileSeek(filehandle,0,FILE_END);
'FILE_END' - cannot convert enum                        282     31
'FILE_END' - improper enumerator cannot be used         282     31

What's wrong with it?

 
Alexey Viktorov:

So I'm thinking: how is it possible to read a structure and three arrays from one file, which you don't know how it was written.

that's what I'm writing, it's easier to check, within 10 minutes.... checked, FileWriteArray() doesn't write the size of the array in the header or the first byte, it means you should write these values yourself

#property copyright "IgorM"
#property link      "https://www.mql5.com/ru/users/igorm"

struct SMystruct
{
   double            d_arr[];
   int               i_arr[];
   uchar             c_arr[];
   int               a;
   double            b;
};

//+------------------------------------------------------------------+
void OnStart()
{
   SMystruct mstruct;
   ArrayResize(mstruct.d_arr,5);    ArrayInitialize(mstruct.d_arr,3.1415926);
   ArrayResize(mstruct.i_arr,3);    ArrayInitialize(mstruct.i_arr,1234567890);
   ArrayResize(mstruct.c_arr,7);    ArrayInitialize(mstruct.c_arr,127);
   mstruct.a = 666;
   mstruct.b = 123.123;
//---  
   if(!SaveMStructToFile("mfile.bin",mstruct)) return;
   SMystruct read_struct;
   if(!LoadMStructInFile("mfile.bin",read_struct)) return;
   Print("1. d_arr");
   ArrayPrint(read_struct.d_arr);
   Print("2. i_arr");
   ArrayPrint(read_struct.i_arr);
   Print("2. c_arr");
   ArrayPrint(read_struct.c_arr);
   printf("mstruct.a = %i , mstruct.b = %f",read_struct.a , read_struct.b);
   
}
//+------------------------------------------------------------------+
bool SaveMStructToFile(const string fname,const SMystruct &data)
{
   int h = FileOpen(fname,FILE_WRITE | FILE_BIN | FILE_COMMON);
   if(h<0) { Print("Write IO Err #",GetLastError()); return(false); }
   FileWriteInteger(h,ArraySize(data.d_arr));   FileWriteArray(h,data.d_arr);
   FileWriteInteger(h,ArraySize(data.i_arr));   FileWriteArray(h,data.i_arr);
   FileWriteInteger(h,ArraySize(data.c_arr));   FileWriteArray(h,data.c_arr);
   FileWriteInteger(h,data.a);
   FileWriteDouble(h,data.b);
   FileClose(h);  
   return(true);
}

bool LoadMStructInFile(const string fname,SMystruct &data)
{
   int h = FileOpen(fname,FILE_READ | FILE_BIN | FILE_COMMON);
   if(h<0) { Print("Write IO Err #",GetLastError()); return(false); } 
   int amount = ArrayResize(data.d_arr,(int)FileReadInteger(h));     FileReadArray(h,data.d_arr,0,amount);
       amount = ArrayResize(data.i_arr,(int)FileReadInteger(h));     FileReadArray(h,data.i_arr,0,amount);
       amount = ArrayResize(data.c_arr,(int)FileReadInteger(h));     FileReadArray(h,data.c_arr,0,amount);
   data.a = FileReadInteger(h);
   data.b = FileReadDouble(h);
   FileClose(h); 
   return(true);
}

2020.03.25 10:18:36.058 tst (EURUSD,H1) 1. d_arr

2020.03.25 10:18:36.058 tst (EURUSD,H1) 3.14159 3.14159 3.14159 3.14159

2020.03.25 10:18:36.058 tst (EURUSD,H1) 2. i_arr

2020.03.25 10:18:36.058 tst (EURUSD,H1) 1234567890 1234567890

2020.03.25 10:18:36.058 tst (EURUSD,H1) 2. c_arr

2020.03.25 10:18:36.058 tst (EURUSD,H1) 127 127 127 127 127

2020.03.25 10:18:36.058 tst (EURUSD,H1) mstruct.a = 666 , mstruct.b = 123.123000

i.e. if array dimensions are dynamic, it means that we write the dimensions of the array themselves when writing, and when reading, we read the value of the array size from the file and set the dimensions of arrays of the structure

in my example 2 structures - one was initialised and written, the second was read, initialisation from file data