BUG in FileWriteString()

 

Would MetaQuotes please explain and/or actually look at the Terminal code and sort out or something...

I have depended on how this function works... is this too much to ask?

Like I read the docs and assumed that such a simple bit of coding would actually work as documented... how wrong [yet again] I have been proved!

Thank you.

Documents state that:

int FileWriteString()
length - The length of the string to be written. If the string length exceeds the given value, it will be truncated. If it is shorter, it will be extended by binary 0s up to the given length

This does not happen - please see below.


examples:

(A) Code:
int iFh=FileOpen("FWS()test",FILE_BIN|FILE_WRITE);
string s="01234567";
Print("StringLen(s)=",StringLen(s),", FileWriteString()=",FileWriteString(iFh, s, 7+StringLen(s)));

Log: 2007.09.20 11:16:25 testBed GBPUSD,M15: StringLen(s)=8, FileWriteString()=15

BINfile:
unsigned char data[] = {
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x00,0x00,0x00,0x00,0x76,0xFA,0xC3,0x77
};

Observation: function only wrote 4 binary zeros before unexpected bytes appear
Expected: write 8 bytes of 1*("01234567") and append 7 binary zeros (15 total length)


(B) Code:
int iFh=FileOpen("FWS()test",FILE_BIN|FILE_WRITE);
string s="0123456701234567012345670123456701234567012345670123456701234567";
Print("StringLen(s)=",StringLen(s),", FileWriteString()=",FileWriteString(iFh, s, 2*StringLen(s)));

Log: 2007.09.20 11:08:34 testBed GBPUSD,M15: StringLen(s)=64, FileWriteString()=128

BINfile:
unsigned char data[] = {
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
0x00,0x00,0x00,0x00,0x76,0xFA,0xC3,0x77,0xF3,0xC1,0xC1,0x77,0x90,0xCF,0xC1,0x77,
0x18,0xBF,0xC1,0x77,0x07,0xC4,0xC2,0x77,0x1B,0xC2,0xC2,0x77,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xD8,0x7A,0x15,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00
};

Observation: function only wrote 4 binary zeros before unexpected bytes appear
Expected: write 64 bytes of 8*("01234567") and append 64 binary zeros (128 in total)

General Observation on above 2 examples:
In both examples the string written followed by 4 binary zeros [only], followed by 4 bytes invalid data: 0x76,0xFA,0xC3,0x77 and in (B) these 4 bytes followed by garbage... IE, FileWriteString() does not do what written on package!!!

 

I'd say you have a bug there... I get different garbage data...

And I increased the size just to see what might happen.

 
    int iFh=FileOpen("FWStest2",FILE_BIN|FILE_WRITE);
    string s="0123456701234567012345670123456701234567012345670123456701234567";
    FileWriteString(iFh,s,10000);
    FileFlush(iFh);
    FileClose(iFh);
 
 
 $ dump "C:\Program Files (x86)\Metatrader\Metatrader\experts\files\F
C:\Program Files (x86)\Metatrader\Metatrader\experts\files\FWStest2:
00000000  3031 3233 3435 3637 3031 3233 3435 3637 0123456701234567
00000010  3031 3233 3435 3637 3031 3233 3435 3637 0123456701234567
00000020  3031 3233 3435 3637 3031 3233 3435 3637 0123456701234567
00000030  3031 3233 3435 3637 3031 3233 3435 3637 0123456701234567
00000040  0000 0000 cd09 bd77 3fc6 ba77 fbd3 ba77 ....M.=w?F:w{S:w
00000050  52c3 ba77 20d0 bb77 33ce bb77 0000 0000 RC:w P;w3N;w....
00000060  0000 0000 98be 2e00 ffff ffff 0000 0000 .....>..........
00000070  0000 0000 c003 0000 0000 0000 484b 2105 ....@.......HK!.
00000080  0400 0000 fe00 0000 0100 0000 0100 0000 ....~...........
00000090  0000 0000 644e 5600 e8be 2e00 ffff ffff ....dNV.h>......
000000a0  0000 0000 0000 0000 0000 0000 0000 0000 ................
000000b0  0000 0000 5840 6502 681a 1503 0000 0000 ....X@e.h.......
000000c0  701e 1903 0000 0000 0100 0000 0000 0000 p...............
000000d0  0100 0000 0000 0000 0000 0000 0000 0000 ................
000000e0  0000 0000 d707 0900 0200 1200 0e00 3600 ....W.........6.
000000f0  1800 b300 7465 7374 6572 0000 0000 0000 ..3.tester......
 
  Addr     0 1  2 3  4 5  6 7  8 9  A B  C D  E F 0 2 4 6 8 A C E
--------  ---- ---- ---- ---- ---- ---- ---- ---- ----------------
00000100  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000110  0000 0000 3134 3a35 343a 3234 2000 0000 ....14:54:24 ...
00000120  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000130  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000140  0000 0000 0000 0000 0000 0000 604e 5600 ............`NV.
00000150  c0be 2e00 ffff ffff 0000 0000 0000 0000 @>..............
00000160  0000 0000 0000 0000 581a 3f05 a84a 0d03 ........X.?.(J..
00000170  b08a 0d03 893e 0200 4800 1303 ef00 0000 0....>..H...o...
00000180  0100 0000 0000 0000 f077 0f00 d707 0900 ........pw..W...
00000190  0400 1400 0000 0500 1d00 1e00 d707 0900 ............W...
000001a0  0400 1400 0700 2f00 0a00 a002 6578 7065 ....../... .expe
000001b0  7274 7300 0000 0000 0000 0000 0000 0000 rts.............
000001c0  0000 0000 0000 0000 0000 0000 3037 3a34 ............07:4
000001d0  373a 3039 2000 0000 0000 0000 0000 0000 7:09 ...........
000001e0  0000 0000 0000 0000 0000 0000 0000 0000 ................
000001f0  0000 0000 0000 0000 0000 0000 f0dc 1203 ............p\..
 
  Addr     0 1  2 3  4 5  6 7  8 9  A B  C D  E F 0 2 4 6 8 A C E
--------  ---- ---- ---- ---- ---- ---- ---- ---- ----------------
... deleted some zero
  Addr     0 1  2 3  4 5  6 7  8 9  A B  C D  E F 0 2 4 6 8 A C E
--------  ---- ---- ---- ---- ---- ---- ---- ---- ----------------
00000500  0000 0000 10bf 2e00 ffff ffff 0000 0000 .....?..........
00000510  0000 0000 0000 0000 0000 0000 28d9 2903 ............(Y).
00000520  6502 0000 7202 0000 0100 0000 38bf 2e00 e...r.......8?..
00000530  ffff ffff 0000 0000 0000 0000 0000 0000 ................
00000540  0000 0000 1e00 0000 0100 0000 60bf 2e00 ............`?..
00000550  ffff ffff 0000 0000 0000 0000 0000 0000 ................
00000560  0000 0000 0000 0000 6080 6502 b88e 1103 ........`.e.8...
00000570  4731 0000 0000 0000 9038 1b03 f900 0000 G1.......8..y...
00000580  8865 1d03 0000 0000 0000 0000 1a05 0000 .e..............
00000590  d707 0900 0400 1400 0000 0500 3500 1901 W...........5...
000005a0  0000 0000 0000 0000 433a 5c50 726f 6772 ........C:\Progr
000005b0  616d 2046 696c 6573 2028 7838 3629 5c4d am Files (x86)\M
000005c0  6574 6174 7261 6465 725c 4d65 7461 7472 etatrader\Metatr
000005d0  6164 6572 0074 6572 6d69 6e61 6c2e 6578 ader.terminal.ex
000005e0  6500 0000 0000 0000 0000 0000 0000 0000 e...............
000005f0  0000 0000 0000 0000 0000 0000 0000 0000 ................
 
  Addr     0 1  2 3  4 5  6 7  8 9  A B  C D  E F 0 2 4 6 8 A C E
--------  ---- ---- ---- ---- ---- ---- ---- ---- ----------------
00000600  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000610  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000620  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000630  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000640  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000650  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000660  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000670  433a 5c50 726f 6772 616d 2046 696c 6573 C:\Program Files
00000680  2028 7838 3629 5c4d 6574 6174 7261 6465  (x86)\Metatrade
00000690  725c 4d65 7461 7472 6164 6572 5c74 6572 r\Metatrader\ter
000006a0  6d69 6e61 6c2e 6578 6500 0000 0000 0000 minal.exe.......
000006b0  0000 0000 0000 0000 0000 0000 0000 0000 ................
000006c0  0000 0000 0000 0000 0000 0000 0000 0000 ................
000006d0  0000 0000 0000 0000 0000 0000 0000 0000 ................
000006e0  0000 0000 0000 0000 0000 0000 0000 0000 ................
000006f0  0000 0000 0000 0000 0000 0000 0000 0000 ................
 
  Addr     0 1  2 3  4 5  6 7  8 9  A B  C D  E F 0 2 4 6 8 A C E
--------  ---- ---- ---- ---- ---- ---- ---- ---- ----------------
00000700  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000710  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000720  0000 0000 0000 0000 0000 0000 0000 0000 ................
00000730  0000 0000 0000 0000 08df 1203 0000 0000 ........._......
00000740  0000 0000 88bf 2e00 ffff ffff 0000 0000 .....?..........
00000750  0000 0000 0000 0000 0000 0000 6828 5700 ............h(W.
00000760  ffff ffff 0000 0000 0000 0000 0000 0000 ................
00000770  0000 0000 0000 0000 0000 0000 0000 0000 ................

 
... the rest of the file is 0000
 

I cannot reproduce this bug

Try to close file explicitly

 

The code I ran to duplicate the problem does have FileClose...

int iFh=FileOpen("FWStest2",FILE_BIN|FILE_WRITE);
string s="0123456701234567012345670123456701234567012345670123456701234567";
FileWriteString(iFh,s,10000);
FileFlush(iFh);
FileClose(iFh);

 

Well then, nice to think that my docs interpreting skills got this one right - for once!

I ran your code verbatum and got all zeros to EOF starting at 0x7e5 (2,021) - yours started at 0x764 (1,892) but certainly near enough...

My dump shows identical content (well, let's say where .exe is - so is good as identical) at 0x670 (1,648)

C:\Program Files\MetaTrader 4\terminal.exe

I was going to program around this, but is clumsy hack to say the least!

Thought of something like a FileWriteString() interface func which would do the binary zeros padding IF length > strLen.

Can only think of using FileWriteArray() with a double[] resized to length and then zero filled, with the remainder bytes (if any left over) written by FileWriteInteger(. ..CHAR_VALUE)

But is time consuming....

 
phy:

The code I ran to duplicate the problem does have FileClose...

int iFh=FileOpen("FWStest2",FILE_BIN|FILE_WRITE);
string s="0123456701234567012345670123456701234567012345670123456701234567";
FileWriteString(iFh,s,10000);
FileFlush(iFh);
FileClose(iFh);


Ok. I've reproduced.

Thinking...

 

Thanks for looking stringo, but...

Just to say again, I also ran exact same code that phy did and results certainly shows the problem. Below is script used - straight copy/paste.

 
int start()
{
    int iFh=FileOpen("FWStest2",FILE_BIN|FILE_WRITE);
    string s="0123456701234567012345670123456701234567012345670123456701234567";
    FileWriteString(iFh,s,10000);
    FileFlush(iFh);
    FileClose(iFh);
 
    return(0);
}//start()

and it just continues like seen in phy's post...

 

let the deep thought be fruitfull !!!!!!!!!!!!!!

cheers

 
Found and fixed. Thank you, ukt and phy
 
stringo:
Found and fixed. Thank you, ukt and phy


stringo Faster than a speeding bullet...

<(-_-)>