Download MetaTrader 5

ReadFile - Move to the next line

To add comments, please log in or register
Find out how to install MetaTrader on Mac OS
Lovro Mirnik
54
Lovro Mirnik 2014.11.19 20:19 

Greetings fellow coders and traders.

I've just recently begun scripting in MQL4 and I find it rather challenging but easy to grasp and understand. Although some steps do give me trouble and I wonder if you'd be able to help me with that.

I am trying to read from a .TXT file with the name of Output.txt that currently contains four lines of code.

The insides of this file look like this:

Date: 19.11.2014

Currently trading: EUR/USD

--------------------

Estimated direction: Bullish

There are exactly 20 of those little dashes on line three. I've been playing around with some basic structures of ReadFile, and I've mashed up something like this.

Reffer to the comments inside the code for the current operation.

int ReadFile;
int StringSize;
string String;
int OnInit()
   {
      ReadFile = FileOpen("Output.txt", FILE_READ | FILE_ANSI); //Opens the file without error
      if (ReadFile != INVALID_HANDLE) //Checks for the error anyways
         {
            while (!FileIsLineEnding(ReadFile)) //Focusing on the first line
               {
                  StringSize = FileReadInteger(ReadFile, INT_VALUE); //Determines the length of the first line?
                  String = FileReadString(ReadFile, StringSize);
                  PrintFormat("001 " + String); //001 allows to separate which operation does what
               }
            while (!FileIsEnding(ReadFile)) //Focus on the entire file because if substituted by !FileIsLineEnding it stays on line one
               {
                  StringSize = FileReadInteger(ReadFile, INT_VALUE); //Again determines the length of each line?
                  String = FileReadString(ReadFile, StringSize);
                  PrintFormat("002 " + String); //002 allows for said separation
               }
            FileClose(ReadFile);
            PrintFormat("File was read.");
         }
      else //Does not come to this
         {
            PrintFormat("File wasn't read.");
         }
      return(INIT_SUCCEEDED);
   }

Now what I would like to do is display the content from only the fourth line, as I deem it most important. One way to solving this I find in this:

int ReadFile;
int StringSize;
string String;
int LineNumber = 1; //Number set to start at one because I've already read the first line
int OnInit()
   {
      ReadFile = FileOpen("Output.txt", FILE_READ | FILE_ANSI);
      if (ReadFile != INVALID_HANDLE)
         {
            while (!FileIsLineEnding(ReadFile))
               {
                  StringSize = FileReadInteger(ReadFile, INT_VALUE);
                  String = FileReadString(ReadFile, StringSize);
                  PrintFormat("001 " + String);
               }
            while (!FileIsEnding(ReadFile)) //Focuses on the rest of the file
               {
                  StringSize = FileReadInteger(ReadFile, INT_VALUE);
                  String = FileReadString(ReadFile, StringSize);
                  LineNumber++; //We moved to the next line
                  if (LineNumber == 4) //When we reach the fourth line
                     {
                        PrintFormat("002 " + String); //Prints the line and it works!
                     }
                  else
                     {
                        //Do Nothing
                     }
               }
            FileClose(ReadFile);
            PrintFormat("File was read.");
         }
      else //Does not come to this
         {
            PrintFormat("File wasn't read.");
         }
      return(INIT_SUCCEEDED);
   }


But I am looking for a way which would read the fourth line without the need for another IF statement. Maybe by adding some int value to StringSize or INT_VALUE?
Or is there a completely different approach that I'm not considering.

Thank you for reading,
Lovro Mirnik

Carl Schreiber
6580
Carl Schreiber 2014.11.19 20:31  

I found it is much easier to read the file completely into one string variable and then split the file by '\n' into an array of lines to walk through:

  int size = (int)FileSize(file_handle);
string str = FileReadString(file_handle,size);
string arrFile[];
int nLines = StringSplit(str,'\n',arrFile);
for(int i=0; i<nLines; i++){ ... }

Lovro Mirnik
54
Lovro Mirnik 2014.11.20 11:28  
Thanks for the reply Gooly, I've got it to work and I must admit, it is less code.
But arrays are something I'll need to dig in some more...
To add comments, please log in or register