Download MetaTrader 5

Reading from TXT file issue

To add comments, please log in or register
NeeZo_NeeZO
48
NeeZo_NeeZO  

Hello guys,

I am trying to read data from a txt file but for some reason it doesn't work. I wanted to share the code with you in case you may have a solution. Thanks a lot in advance.

So the file is a TXT file having only 2 integer values of 1 and 10 separated by XX. So the file content is like this: 1XX10

Now here is the code:

string         file_name="File.txt";
int            handle=FileOpen(file_name,FILE_TXT|FILE_READ|FILE_ANSI,"XX");
               if(handle>0)
               {
               int    A=FileReadInteger(handle);
               int    B=FileReadInteger(handle);
               }
whroeder1
15727
whroeder1  
Perhaps you should read the manual.
  1. int            handle=FileOpen(file_name,FILE_TXT|FILE_READ|FILE_ANSI,"XX");
    FILE_TXT a simple text file (the same as csv, but the delimiter is not taken into account) File Functions / FileOpen - Reference on algorithmic/automated trading language for MetaTrader 5

  2. int  FileOpen(
       string  file_name,           // File name
       int     open_flags,          // Combination of flags
       short   delimiter='\t',      // Delimiter
       uint    codepage=CP_ACP      // Code page
       );
    The third argument is a single character, you can't use a string and you can't use multiple characters with CSV.

  3.                if(handle>0)
    Check your return codes for errors and report them. What are Function return values ? How do I use them ? - MQL4 forum
    Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  4. Read the entire line and use StringSplit and StringToInteger or change the file format.

NeeZo_NeeZO
48
NeeZo_NeeZO  
whroeder1:
Perhaps you should read the manual.
  1. FILE_TXT a simple text file (the same as csv, but the delimiter is not taken into account) File Functions / FileOpen - Reference on algorithmic/automated trading language for MetaTrader 5

  2. The third argument is a single character, you can't use a string and you can't use multiple characters with CSV.

  3. Check your return codes for errors and report them. What are Function return values ? How do I use them ? - MQL4 forum
    Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  4. Read the entire line and use StringSplit and StringToInteger or change the file forma

Thanks a lot for your reply. I was wondering if I could create TXT or CSV files where values within the line are separated by a specific character (tab here). Something like this:

FIle.TXT

10   Hello   1000  2

10   Hello   350    3

and then read the values one by one. Meaning I read the first value and move the pointer to the next and i read that and so on and so forth. Is that doable?

Sorry if my questions seem so silly to you. I read the manual few times but since I am new to programming I still get easily confused. Thanks in advance!

Nima

NeeZo_NeeZO
48
NeeZo_NeeZO  

could I ask you for a favor?

All I want to do is to read a file of data (which I can have in either txt or csv format) and store them in arrays. I have string, datetime, and double values in it. Could you point me to the right direction? Like a good tutorial on it? Because the reference doesn't do it for me :(

dennis681
85
dennis681  

I needed to read values from a txt file with a series of string,number...  string, number...  etc...   by processing the Pair (string) and it's "code" (number).  When the file was being written to, I used FileWriteString(file_handle,"\n"); to create a linefeed (Enter) between each value.  Are arrays needed?  Since each 2-lines are processed one at a time, maybe not, but I haven't finished the project, so not sure if they'll be needed (in other words, I'm a hack).

      string InpFileName="Action File.txt";

      file_handle=FileOpen(InpFileName,FILE_READ|FILE_CSV);    //CSV has highest priority flag anyway and it works 

      int    str_size; 

      int a1,b1;

      b1=0;

      while(!FileIsEnding(file_handle))  // this reads each two lines of values, one at a time ie "USDJPY" (Enter) "3" ... process that, then read in next two lines

      { 

         b1++;

         for(a1=1;a1<=2;a1++)

         {

            str_size=FileReadInteger(file_handle,INT_VALUE); 

            str[b1][a1]=FileReadString(file_handle,str_size); 

            if(a1==1) z1=str[b1][a1];

            if(a1==2) 

            {

               b2=(int)str[b1][a1];

               d1(z1,b2);            //call to d1 sends a string, number to be further processed

               Sleep(10000);       // 10 sec timer

            }

         }

      FileClose(file_handle); 

    }

NeeZo_NeeZO
48
NeeZo_NeeZO  
whroeder1:
Perhaps you should read the manual.
  1. FILE_TXT a simple text file (the same as csv, but the delimiter is not taken into account) File Functions / FileOpen - Reference on algorithmic/automated trading language for MetaTrader 5

  2. The third argument is a single character, you can't use a string and you can't use multiple characters with CSV.

  3. Check your return codes for errors and report them. What are Function return values ? How do I use them ? - MQL4 forum
    Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  4. Read the entire line and use StringSplit and StringToInteger or change the file format.

THANKS man.. This totally solved my issue.
To add comments, please log in or register