File Operations: Reading CSV

 

Good Day, MQL5 Community!

I've encountered a problem in my attempt to read from .csv files using FileReadString() or FileReadInteger() functions. The functions work beautifully if I use MQL5 to write to a file, however, if the data is written to csv by other means (to be more specific, I'm using C# FileHelpers library) the function returns very meaningful   ㈢㠹㌬㔰ഢ∊㌱ⰲ㐱∶਍㌢ㄴ㌬㘷ഢ∊〲ⰹ㈲∷ etc.  I was wondering if there is a way to format csv for it to be readable by MQL5? If somebody had a similar problem in the past or simply is a very smart person, could you please help me resolve this issue?

My csv content generated by C# is similar to this (I tried inserting tabs to match the format of MQL5 written files and changing Codepages in FileOpen - nothing worked):

298,305
132,146
341,376
209,227

Thank you,

Burton

 P.S. Speaking of csv, if anyone knows how to programmatically delete the last empty row the FileWrite() method leaves when it finishes writing, I would really appreciate it if you tell me.

Documentation on MQL5: File Functions / FileReadInteger
  • www.mql5.com
File Functions / FileReadInteger - Documentation on MQL5
 
Burton:

Good Day, MQL5 Community!

I've encountered a problem in my attempt to read from .csv files using FileReadString() or FileReadInteger() functions. The functions work beautifully if I use MQL5 to write to a file, however, if the data is written to csv by other means (to be more specific, I'm using C# FileHelpers library) the function returns very meaningful   ㈢㠹㌬㔰ഢ∊㌱ⰲ㐱∶਍㌢ㄴ㌬㘷ഢ∊〲ⰹ㈲∷ etc.  I was wondering if there is a way to format csv for it to be readable by MQL5? If somebody had a similar problem in the past or simply is a very smart person, could you please help me resolve this issue?

My csv content generated by C# is similar to this (I tried inserting tabs to match the format of MQL5 written files and changing Codepages in FileOpen - nothing worked):

298,305
132,146
341,376
209,227

Thank you,

Burton

 P.S. Speaking of csv, if anyone knows how to programmatically delete the last empty row the FileWrite() method leaves when it finishes writing, I would really appreciate it if you tell me.

Hi Burton,

I don't know anything about C#, why don't you open that C#'s csv with notepad. AFAIK what we see in notepad is what MQL5 FileReadString() read.

:) 

 
onewithzachy:

Hi Burton,

I don't know anything about C#, why don't you open that C#'s csv with notepad. AFAIK what we see in notepad is what MQL5 FileReadString() read.

:) 

Well, I did try that and many other things (like typing integers into csv with a keyboard in a notepad, inserting tabs between them, changing separators, changing file handle properties, changing extension to .txt etc.), the result is same old  ㈳ⰴ㔳ഴ㈊㜲㈬㔴਍㈱ⰳ㔱ശ㘊ⰷ㔸. The thing that really pisses me off is that I cannot find any differences between my file and the one generated by mql5, neither in its content nor in its properties. Only that FileReadString() has no problems reading mql5 csv, and spits out gibberish when it attempts to read any other. This is the final stage of a very long project I was working on, and this stupid bug is driving me nuts. Please, help, anyone!
 
Burton:
Well, I did try that and many other things (like typing integers into csv with a keyboard in a notepad, inserting tabs between them, changing separators, changing file handle properties, changing extension to .txt etc.), the result is same old  ㈳ⰴ㔳ഴ㈊㜲㈬㔴਍㈱ⰳ㔱ശ㘊ⰷ㔸. The thing that really pisses me off is that I cannot find any differences between my file and the one generated by mql5, neither in its content nor in its properties. Only that FileReadString() has no problems reading mql5 csv, and spits out gibberish when it attempts to read any other. This is the final stage of a very long project I was working on, and this stupid bug is driving me nuts. Please, help, anyone!

Pardon me, only if you don't mind, could you insert your file operation code here (use SRC button to insert 'em) and if possible attach some of your csv data file that generated by your C#, just make sure that your super secret data is not attached along way ;). See if any of us around here can replicate the same problem or ... thank god it's only you :).

Have you try it with MT4/MQL4, see if the same problem occurs ? Another thing that I kinda bored to ask, what's your OS ?, if it is Vista/7, surely you're not install MT5 in C:\Program Files\..., right ?.

Sorry if I ask a lot, just trying to eliminate some possibility :)

Cheers up, Burton. 

 

 

 

Not many people are polite these days, especially, when they attempt to make a joke.  :) My system is Win7 64-bit. MT5 is, in fact, located in C:\Program Files\..., since it's a 64 version. CSV files are located in ...\MT5\Files\TrainingSet\. I'm attaching one, please see below. The code, I'm using to open it, is not mine, I found it in one of the articles, though it's pretty straight forward and is able to locate and open the files (and even read the ones written by MT5).
//--- global variables
string data[10][2];
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    
    string separator = ",";
   int m_handle=-1;
      string m_filename="TrainingSet\Results.txt";
      m_handle=FileOpen(m_filename,FILE_CSV | FILE_READ,separator,CP_ACP);
      if (m_handle<0)
       {
          Print("I can't open the file.");
       } 
      else 
        Print("File successfully open.");
        
        
      
      for(int i = 0; i<10;i++)
      {
        data[i][0] = FileReadString(m_handle,10);
        Print(data[i][0]);
      }
               
       FileClose(m_handle); 
       
          
   
  }
I'm sorry, but I have never worked with MT4, and I do apologize for not stating my question in an informative manner.
Files:
Results.txt  1 kb
 

Burton:

Not many people are polite these days, especially, when they attempt to make a joke.  :) My system is Win7 64-bit. MT5 is, in fact, located in C:\Program Files\..., since it's a 64 version. CSV files are located in ...\MT5\Files\TrainingSet\. I'm attaching one, please see below. The code, I'm using to open it, is not mine, I found it in one of the articles, though it's pretty straight forward and is able to locate and open the files (and even read the ones written by MT5). 

I'm sorry, but I have never worked with MT4, and I do apologize for not stating my question in an informative manner.

No need to apologize, I probably unable to solve this err anyway - LOL - hopes other forumers also helps.

Oh dear, Win 7 64 ... eh, hope not another err https://www.mql5.com/en/forum/6895

 
Burton:

Open your file in ANSI mode.

m_handle=FileOpen(m_filename,FILE_CSV|FILE_ANSI|FILE_READ,separator,CP_ACP);

 

 

Ah ... thank you avoitenko, for helping us.

Hi Burton, try this script below, I highlight some changes I made. There's no problem with MT4 and I found no error running this in my MT5, hope no error when you running this script coz you have different system than mine.  I'm using Vista HB 32 bit and install all MT in  C:\Program Files\...  but I change the security permission.

Since you have 10 rows and 20 data, I don't think the data array will hold it if we use the original script. And regarding your other Q (delete the last empty row), my answer is, I dunno :D.

Anyway, whatever it is, have fun Burton :)

:D

@MetaQuotes - I change the background color and the codes shrinks

//--- global variables
string data[10][2];
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   string separator=",";
   int m_handle=-1, count, line_count = 1;
   string m_filename="TrainingSet\Results.txt";
   
   m_handle=FileOpen(m_filename,FILE_CSV|FILE_ANSI|FILE_READ,separator,CP_ACP);
   if(m_handle<0)
     {
     Print("I can't open the file.");
     }
     else
     {
     Print("File successfully open.");
     while (FileIsEnding(m_handle) == false)
       {
       data[count][0]=FileReadString(m_handle, 10);
       data[count][1]=FileReadString(m_handle, 10);
       Print("Line number "+IntegerToString(line_count)+", data 1 = "+data[count][0]);
       Print("Line number "+IntegerToString(line_count)+", data 2 = "+data[count][1]);
       if (FileIsLineEnding(m_handle) == true)line_count ++;
       count ++;
       }
     /*
     for(count = 1; count <= 10;count ++)
       {
       Print (""+IntegerToString(count)+" ta da ==>> "+data[count - 1][0]);
       Print (""+IntegerToString(count)+" ta da ==>> "+data[count - 1][1]);
       }
      */
      FileClose(m_handle);
     }
   PlaySound("wait.wav");
  }


 
The code executes perfectly. Thank you for taking the time to help me. I'm new to the forum, so if there's a way to give you guys digital thumbs up, let me know how.
Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Standard Constants, Enumerations and Structures / Objects Constants / Object Types - Documentation on MQL5
 
Burton:

The code executes perfectly. Thank you for taking the time to help me. I'm new to the forum, so if there's a way to give you guys digital thumbs up, let me know how.

I was eagerly waiting to reply "Thank God it's only Burton", ... turned out, ... huh.

Just kidding Burton, people are too serious over here in this forum, seriously no kidding :) 

Have fun.

:D 

 
Comments that do not relate to this topic, have been moved to "How to delete file content?".
Reason: