Download MetaTrader 5

Maximum string length read by FileReadString

To add comments, please log in or register
Paul
784
Paul  

Is there any reason for the 4093 character maximum length of a string read by FileReadString from a BIN file?

Slava
Moderator
6961
Slava  

1. May be that zero character in the file terminated your string

2. What the error gets GetLastError()? 

Paul
784
Paul  
stringo:

1. May be that zero character in the file terminated your string

2. What the error gets GetLastError()? 

Apologies for not looking at the GetLastError() before - see below

int handle = FileOpen("test.txt",FILE_READ|FILE_BIN);
// valid handle from a 4MB text file produced using notepad.exe

str = FileReadString(handle,100000);
Print(GetLastError()); // this prints 4004 - not enough memory

Print(StringLen(str)); // this prints 4093 - expect 100000

 

Slava
Moderator
6961
Slava  

You can try to read string's remains and concatenate they

Paul
784
Paul  
stringo:

You can try to read string's remains and concatenate they

Yes, the following works well to read a 4MB string

// assumes handle is FILE_BIN
string FileReadLargeString(int handle)
  {
   string strLarge="";
   do
     {
      ResetLastError();
      string str=FileReadString(handle,4000);
      if(GetLastError()!=0) break;
      StringConcatenate(strLarge,strLarge,str);
     }
   while(GetLastError()==0 && !FileIsEnding(handle));
   return(strLarge);
  }

 

To add comments, please log in or register