Getting the content of an mq4 file within my mq4 program

 

I have a file called test.mq4.

I need to get its content as a string in my other mq4 program.

The test.mq4 is an UTF-16 file. If I convert it to UTF-8 (by an external text editor), I can get its content like this:

   uchar filedata[];
   int handle=FileOpen("test.mq4",FILE_READ|FILE_BIN);
   if(handle!=INVALID_HANDLE)
     {
      FileReadArray(handle,filedata);
     }
   FileClose(handle);
   string filetext=CharArrayToString(filedata);

How can I get the content of the UTF-16 file as a string (ie, without using an external conversion to UTF-8)?

 
It's a text file not binary. Open it as a TXT|UNICODE
          FileOpen - File Functions - MQL4 Reference
 
William Roeder:
It's a text file not binary. Open it as a TXT|UNICODE
          FileOpen - File Functions - MQL4 Reference

I did it this way, but it only gives the first line of the mq4:

   int handle=FileOpen("test.mq4",FILE_READ|FILE_TXT|FILE_UNICODE);
   if(handle!=INVALID_HANDLE)
     {
      string filetext=FileReadString(handle,(int)FileSize(handle));      
     }  
   FileClose(fileHandle);
What did I wrong? 
 
linux80s:

I did it this way, but it only gives the first line of the mq4:

What did I wrong? 
   int handle = FileOpen("test.mq4",FILE_READ|FILE_TXT|FILE_UNICODE);
   
      if(handle != INVALID_HANDLE){
      
         while(!FileIsEnding(handle)){
         
            string filetext = FileReadString(handle, FileReadInteger(handle, INT_VALUE));
            
            PrintFormat(filetext);
         }
      FileClose(handle);
   }
 
Pavel Shutovskiy:

Thank you Pavel, but unfortunately it doesn't work.

It only gives the first line of the text.

 
linux80s:
Thank you Pavel, but unfortunately it doesn't work.

what are the errors in the log? 

this example should work

 
Pavel Shutovskiy:

what are the errors in the log? 

this example should work

Update: my fail, sorry. The code works! Thank you Pavel!

 
linux80s:

Update: my fail, sorry. The code works! Thank you Pavel!

New problem: I tried this code on a UTF-8 text and it doesn't work (gives a gibberish result).

Since I don't know in advance that the source file is UTF-8 or UTF-16, I need a solution that works with both case.

Or

Is it possible to detect by MQL if a file is UTF-8 or UTF-16?

Have you any idea?

 
linux80s:

New problem: I tried this code on a UTF-8 text and it doesn't work (gives a gibberish result).

Since I don't know in advance that the source file is UTF-8 or UTF-16, I need a solution that works with both case.

Have you any idea?

FILE_ANSI

 
Pavel Shutovskiy:

FILE_ANSI

In the meantime I get it also, thank you!

Another question: is it possible to cross-convert from UTF8 to UTF16 and vice versa by MQL?

 
linux80s:

Another question: is it possible to cross-convert from UTF8 to UTF16 and vice versa by MQL?

change the encoding by overwriting the file

Reason: