MQL% FileWriteStruct generates no output to file

 

My EA has the following function:

int GetHistories(ENUM_TIMEFRAMES timeframe)
{
   MqlRates Rates[];
   int copied = 0;
   ResetLastError();
   string symbx=Symbol();
   for(int x=0; x<mc.arrsymbx && !IsStopped(); x++) 
    {
      symbx=mc.DIRI[x];
      string InpFileName = symbx + ".csv";
      int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI|FILE_TXT, ',');
      if(file_handle!=INVALID_HANDLE)
        {
         PrintFormat("%s file is available for writing",InpFileName);
         PrintFormat("File path: %s\\MQL5\\Files\\Data\\",TerminalInfoString(TERMINAL_DATA_PATH));
         MqlDateTime startTM;
         startTM.year= 2023;
         startTM.mon = 1;
         startTM.day = 1;
         startTM.hour= 0;
         startTM.min = 0;
         startTM.sec = 0;
         datetime now=TimeCurrent();
         datetime start = StructToTime(startTM);
         copied = CopyRates(symbx, timeframe, start, now, Rates);
         FileWrite(file_handle,Rates[0].time,Rates[0].open);
         FileWrite(file_handle,Rates[1].time,Rates[1].open);
         FileWriteStruct(file_handle,Rates[0]);
         FileWrite(file_handle,Rates[2].time,Rates[2].open);
         FileFlush(file_handle);
         //--- close the file
         FileClose(file_handle);
         PrintFormat("Data is written, %s file is closed",InpFileName);
        }
      else
         PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
      Print("GetHistory: symbx = ", symbx, " copied = ", copied);
      Print("time[0] = ", Rates[0].time);
      Print("Open[0] = ", Rates[0].open);
   }
   return copied;
}


The array mc,DIRI has 10 element with FOREX pair symbols in each element. The for statement iterates through each one.

For each symbol, it FileOpen a CSV output file for FILE_WRITE.

It executes CopyRaies for each symbol into the structure MqlRates Rates[].

There are 3 FileWrite statements that begin to write the first 3 rows from the Rates[]. This demonstraes that the Rates[] has good data in it.

The FileWriteStruct(file_handle, Rates[0]) statement should write the entire first row of data from Rates[0], but it generates no file output.

Each csv file only has 3 lines in it that look like:

2023.01.02 07:00:00,1.07016

2023.01.02 07:15:00,1.06969

2023.01.02 07:30:00,1.06929


Excel recognses the files as 3 rows of good CSV data.


Why does the FileWriteStruct generate no file output while the 3 FileWrite statements work OK.

I would like to save effort by using FileWriteStruct statement instead.


THANKS.

What am I doing wrong?


 
My take is FileWriteStruct only works if the struct has native C++ types as fields.(long, int, double,...) MqlRates has a datetime field which is not a native type. That is why it is not working.
 
Yashar Seyyedin #:
My take is FileWriteStruct only works if the struct has native C++ types as fields.(long, int, double,...) MqlRates has a datetime field which is not a native type. That is why it is not working.
Sorry but that doesn't make sense. Of course datetime as a native type, as documented the only type restriction is "The structure should not contain strings, dynamic arrays or virtual functions."
 
Don Baechtel:

THANKS.

What am I doing wrong?

 int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI|FILE_TXT, ',');

Please read the documentation before posting.

FileWriteStruct

The function writes into a bin-file contents of a structure passed as a parameter, starting from the current position of the file pointer.

Incidentally a file can have 1 type ONLY, it's either CSV or TXT or BIN but not both at the same time.

 
You didn't specify what InpDirectoryNane is, not wheither it is a subdirectory of «DataFolder».