Reading the Lines of CSV file using MQL5

 

I am trying to read the last line time value from a csv file. Check the following:

2018-07-26 11:04:00 1.17272 1.17275 1.17267 1.17272
2018-07-26 11:05:00 1.17272 1.17273 1.17265 1.17268
2018-07-26 11:06:00 1.17268 1.17273 1.17261 1.17264

The above is sample data. I have tried the following code and the result is as follows:

int file = FileOpen("latest.csv",FILE_READ|FILE_SHARE_READ|FILE_CSV|FILE_COMMON);
   if(file != INVALID_HANDLE)
   {
      FileSeek(file,-100,SEEK_END);

      while(!FileIsLineEnding(file))
      {
         Print(FileReadString(file));
      }
   }

   FileClose(file);

Output:

8-07-26 11:06:00
1.17268
1.17273
1.17261
1.17264

I m not getting the complete date value. Even if I try to increase the offset of the FileSeek()function.

Kindly, let me know how is it possible to read the csv file's last line.

 
Search - MQL5.community
Search - MQL5.community
  • www.mql5.com
Searching is based on morphology and is insensitive to case. All letters, no matter of their case, will be processed as lowercase. By default, our search engine shows pages, that...
 
Marco vd Heijden:
Hello there are 244 pages on csv here: https://www.mql5.com/en/search#!keyword=csv&module=mql5_module_forum

Thank you for your references. But it could have been more helpful if you have come up with something that will help. I have read many articles here but none was helpful to me. Hence, I have asked the question here. Please help me.

 

I do not know the answer directly maybe you have to specify the delimiter see: https://www.mql5.com/en/docs/files/fileopen

It seems to be a space in stead of the regular comma as in csv comma separated values.

You want to use commas separated values without using a comma to separate them.

You could try to specify the delimiter as being a space.

int  FileOpen( 
   string  file_name,           // File name 
   int     open_flags,          // Combination of flags 
   short   delimiter='\t',      // Delimiter 
   uint    codepage=CP_ACP      // Code page 
   );
Documentation on MQL5: File Functions / FileOpen
Documentation on MQL5: File Functions / FileOpen
  • www.mql5.com
[in]  The name of the file can contain subfolders. If the file is opened for writing, these subfolders will be created if there are no such ones. [in]  value to be used as a separator in txt or csv-file. If the csv-file delimiter is not specified, it defaults to a tab. If the txt-file delimiter is not specified, then no separator is used. If...
 
jaffer wilson:

Kindly, let me know how is it possible to read the csv file's last line.

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Особенности языка mql5, тонкости и приёмы работы

fxsaber, 2018.04.06 17:08

// Заполнение массива строками из файла - альтернатива
int FileToStrings2( const string FileName, string &Str[] )
{
  uchar Bytes[];
  
  return(FileLoad(FileName, Bytes) ? StringSplit(CharArrayToString(Bytes), '\n', Str) : 0);
}
void OnStart()
{
  string Str[];
  int Amount = FileToStrings2("Test.csv", Str);
  
  if (Amount)
    Print(Str[Amount - 1]);
}
 
Marco vd Heijden:

I do not know the answer directly maybe you have to specify the delimiter see: https://www.mql5.com/en/docs/files/fileopen

It seems to be a space in stead of the regular comma as in csv comma separated values.

You want to use commas separated values without using a comma to separate them.

You could try to specify the delimiter as being a space.

I tried it sir. But didn't worked for me.

 

Show the code.

The chance is extremely slim that the answer is not in one of these 244 search result pages.

I used to be able to solve 90% of the issues by just doing some searches.

You can also scoop over one of the quality articles for example this: https://www.mql5.com/en/articles/2720

Scroll down to the part: 

Reading a text file with separators

MQL5 Programming Basics: Files
MQL5 Programming Basics: Files
  • www.mql5.com
Like many other programming languages, MQL5 features the functions for working with files. Although working with files is not a very common task when developing MQL5 Expert Advisors and indicators, each developer faces it sooner or later. The range of issues that require working with files is wide enough. It includes generating a custom trading...
Reason: