It doesn't seem a bug, I believe the documentation should be modified, the current description says:
- If you specify a size greater than the length of the string, the string is filled by the appropriate number of zeros.
Please note the file is created with FILE_UNICODE.
The length of "1234567" is 7 which means 14 bytes to write,and the length parameter is set to 64 which means 128 bytes to write, in this case,it should write 128 bytes,actually it wrote 135 bytes.
128 + 7 = 135, obviously the function added additional 7 bytes
https://docs.mql4.com/files/filewritestring
Note
Note that when writing to a file opened by the FILE_UNICODE flag (or without a flag FILE_ANSI), then the number of bytes written will be twice as large as the number of string characters written. When recording to a file opened with the FILE_ANSI flag, the number of bytes written will coincide with the number of string characters written.
- docs.mql4.com
Tested in MT5, the function FileWriteString works well.
//+------------------------------------------------------------------+ //| 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 } //+------------------------------------------------------------------+
It doesn't seem a bug, I believe the documentation should be modified, the current description says:
- If you specify a size greater than the length of the string, the string is filled by the appropriate number of zeros.
The total written bytes should be 128, the filled zero is 64*2 - 7*2 = 114 .
Please note the file is created with FILE_UNICODE.
The length of "1234567" is 7 which means 14 bytes to write,and the length parameter is set to 64 which means 128 bytes to write, in this case,it should write 128 bytes,actually it wrote 135 bytes.
128 + 7 = 135, obviously the function added additional 7 bytes
The length is different from bytes, the length of "1234567" is 7, but in case of bytes, it takes 14 bytes when encoded in UNICODE.
I agree that when the third parameter is greater than the length of the string, it should add zeros of the difference between the length of the string and the provided parameter, multiplied by 2 as it is to be encoded in UNICODE, like what is done in MT5, but I still think that the documentation needs clarification and the word "appropriate" is ambiguous, both in MQL4 and MQL5 documentation.
And current behavior of the function which is different in MQL4 compared to MQL5, should be changed to work the same.
If you want this issue to be fixed, it is required to post in Russian forum where Metaquotes developers usually read, and I'm not sure if they change it as the development of MT4 is stopped since long time ago, you should consider a work-around for this issue, like StringToShortArray and resize the array, add zeros of your choice, then write this array to the file.
Thanks for your advice.
I have posted it in Russsian forum.
- 2021.05.25
- www.mql5.com
Thanks for your advice.
I have posted it in Russsian forum.
When you post on Russian forum you should use the translation tool, it's more respectful for Russian readers.
And the appropriate topic to post bug reports is this one https://www.mql5.com/ru/forum/369007/page2
- 2021.05.16
- www.mql5.com
... FileWriteString(handle,str, StringLen(str)); ... str=FileReadString(handle,StringLen(str));everything works as it should ;-)
When you post on Russian forum you should use the translation tool, it's more respectful for Russian readers.
And the appropriate topic to post bug reports is this one https://www.mql5.com/ru/forum/369007/page2
Thanks.
Good advice.
I have reedited the post with Russian using the translation tool.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Write string with FILE_UNICODE flag, as shown in the code, additional 7 bytes are written.