Bug in FileWriteString (MQL4)

 

//

uint  FileWriteString(
   int           file_handle,    // File handle
   const string  text_string,    // string to write
   int           length=-1       // number of symbols
   );

При записи текста в файл Unicode, если указана длина параметра, записанные байты должны быть вдвое больше длины параметра.

Но фактические байты - это удвоенная длина параметра плюс количество символов в тексте, если длина параметра больше, чем количество символов в тексте.

Этот код показывает ошибку.

//+------------------------------------------------------------------+
//|                                     test_FileWriteString_bug.mq4 |
//|                                           Copyright 2021,fxMeter |
//|                            https://www.mql5.com/en/users/fxmeter |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021,fxMeter"
#property link      "https://www.mql5.com/en/users/fxmeter"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
   string  str = "1234567";
   int     num = 32;//0x20
   string  file = WindowExpertName()+".bin";

//---write str and num
   int handle=FileOpen(file,FILE_BIN|FILE_WRITE|FILE_UNICODE);
   FileWriteString(handle,str,64);  //128 bytes
   printf("1. file pointer =%d",FileTell(handle));//135 ?  it should be 128, additional 7 bytes are filled
   FileWriteInteger(handle,num,INT_VALUE);
   printf("file size = %d",FileSize(handle)); //139
   FileClose(handle);

//---read
   printf("--------------");

   handle=FileOpen(file,FILE_BIN|FILE_READ|FILE_UNICODE);


   str=FileReadString(handle,64);
   printf("2. file pointer =%d",FileTell(handle));//128
   printf("str =%s",str);

   num=FileReadInteger(handle,INT_VALUE);
   printf("3. file pointer=%d",FileTell(handle));//132
   printf("num = %d",num); // num = 0

}
//+------------------------------------------------------------------+




 

bug

bug2

//...

 

Протестировано в MT5, функция FileWriteString работает хорошо.


//+------------------------------------------------------------------+
//|                                     test_FileWriteString_bug.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
   string  str = "1234567";
   int     num = 32;//0x20
   string  file = MQLInfoString(MQL_PROGRAM_NAME) + ".bin";
//---write str and num
   int handle = FileOpen(file,FILE_BIN | FILE_WRITE | FILE_UNICODE);
   FileWriteString(handle,str,64);  //128 bytes
   printf("1. file pointer =%d",FileTell(handle));//128
   FileWriteInteger(handle,num,INT_VALUE);
   printf("file size = %d",FileSize(handle)); //132
   FileClose(handle);
//---read
   printf("--------------");
   handle = FileOpen(file,FILE_BIN | FILE_READ | FILE_UNICODE);
   str = FileReadString(handle,64);
   printf("2. file pointer =%d",FileTell(handle));//128
   printf("str =%s",str);
   num = FileReadInteger(handle,INT_VALUE);
   printf("3. file pointer=%d",FileTell(handle));//132
   printf("num = %d",num); // num = 32
}
//+------------------------------------------------------------------+


nobug


nobug2

//...

 

Оригинальный пост на английском форуме

https://www.mql5.com/en/forum/369872

Bug in FileWriteString (MQL4)
Bug in FileWriteString (MQL4)
  • 2021.05.24
  • www.mql5.com
Write string with FILE_UNICODE flag, as shown in the code, additional 7 bytes are written...