FileReadString in binary file problem ? [FIXED]

To add comments, please log in or register
David Saulnier
345
David Saulnier  
   string cfgFilename = "myfile.cfg";
   string page = "";
   int h = FileOpen(cfgFilename, FILE_READ|FILE_BIN);
   if(h < 1)
   {
         Alert("[ERROR] "+Symbol()+" Cannot read file: "+cfgFilename);
         return (false);
   }
   else
      {
         int len = FileReadInteger(h, LONG_VALUE);
         page = FileReadString(h, len);
         FileClose(h);
         if(StringLen(page) != len)
            Alert("[ERROR] Ctrl Config: page length = "+StringLen(page)+" expected: "+len);
      }  

I have a problem with the expected string len to be read by FileReadString:

Expected string length (int len returns 7512)

then

page = FileReadString(h, len);

but StringLen(page) = 7167 (or sometime 4095 randomly) instead of 7512 !

any idea?

ps: I cannot/don't want to use FILE_READ|FILE_CSV as I've mixed binary

Simon Gniadkowski
17653
Simon Gniadkowski  
DavidS777:

I have a problem with the expected string len to be read by FileReadString:

Expected string length (int len returns 7512)

then

but StringLen(page) = 7167 (or sometime 4095 randomly) instead of 7512 !

any idea?

ps: I cannot/don't want to use FILE_READ|FILE_CSV as I've mixed binary


This might help or explain a little: https://www.mql5.com/en/forum/147705
qjol
3195
qjol  
if(h < 1) ??

whats the FileSize(h) ?

David Saulnier
345
David Saulnier  

I'm gonna test this.

when I create a string zz ="....... over 4k characters".... the editor crash.

So 'Ill try with a stringconcatenate and a loop...


thanks for help

David Saulnier
345
David Saulnier  
qjol:

whats the FileSize(h) ?


FileSize == 7516 (int len 4+ string 7512)
David Saulnier
345
David Saulnier  

finally I'm not sure this is necessary to test because when I write the string of 7512 bytes to the file with FileWriteString it works.

only the read failed. that's strange

David Saulnier
345
David Saulnier  
int len = FileReadInteger(h, LONG_VALUE);
         for(int z=0;z<len;z++)
            page = page+" ";
         Alert("===>"+StringLen(page));

page is well 7512, so I think it is possible to have over 4k string.

Maybe there is a limitation for FileReadString ? (it reads only 7167 bytes.)

Ex Ovo Omnia
3480
Ex Ovo Omnia  
DavidS777:

page is well 7512, so I think it is possible to have over 4k string.

Maybe there is a limitation for FileReadString ? (it reads only 7167 bytes.)



Perhaps you have 0x00 in the string to be read. The MQL string is always truncated if this value appears.
David Saulnier
345
David Saulnier  

Ok I found the problem. FileReadString is/seems to be limited to read APPROXIMATIVELY packet of 4096 bytes.

It should be good to mention this into the documentation of FileReadString, here a nasty but working fix for those interested:

      string page = "";
      int h = FileOpen(cfgFilename, FILE_READ|FILE_BIN);
      if(h < 1)
      {
         Alert("[ERROR] "+Symbol()+" Cannot read file: "+cfgFilename);
      }
      else
      {
         // Header
         page = "";
         int len = FileReadInteger(h, LONG_VALUE);
         int toRead = len;
         int packet = 4000;   // I know 4k = 4096 but I took a little margin because sometimes it reads 4095 max...
         while(toRead != 0)
         {
            if(FileIsEnding(h)) break;
            string buf = FileReadString(h, packet);
            /*
            int err=GetLastError();
            if(err != 0)
               Alert("[ERROR] "+err+": "+ErrorDescription(err));
            */
            page = page+buf;
            toRead -= packet;
            if(toRead < packet)
               packet = toRead;
            
         }
         FileClose(h);
         if(StringLen(page) != len)
            Alert("[ERROR] "+Symbol()+" Ctrl Config: page length = "+StringLen(page)+" expected: "+len+", 10 first chars: "+StringSubstr(page, 0, 10));
      }   
Stanislav Korotky
27985
Stanislav Korotky  

FYI, the problem affects MT5.

This is especially annoying because the thing has been working without limitation in MT4 in my case. So for me it's strange that it's reported in MT4 section.

To add comments, please log in or register