MT4-Read a CSV file and load the info in a struct

 

I need to read a CSV file like the lines bellow and load in the a structure to access bellow.


What is the best form (function maybe) to read and save the data in this structure, I have many lines to read.

The file is stored in "..\MQL4\Files\file.csv" directory.


I have a CSV file with many lines:

EURUSD,1,sddw32@gmail.com,274653782,0,true

USDJPY,3,dffdff@gmail.com,274653782,0,false



struct line_read
  {
   string         symbol;  // symbol
   int            code1;   // field 2
   string         email;   // email
   int            code2;   // field 4
   int            code3;   // field 5
   string         status;  // field 6
  };


 
So what is your question ?
 
Marco vd Heijden:
So what is your question ?

I need a function to read a file and save in one structure.

 
You haven't stated a problem, you stated a want.
You have only four choices:
  1. Search for it,
  2. Beg at Will code your trading systems for free - Free Expert Advisors - Trading Systems - MQL5 programming forum, or Coding help - MQL4 and MetaTrader 4 - MQL4 programming forum, or Need help with coding - General - MQL5 programming forum, or Free MQL4 To MQL5 Converter - General - MQL5 programming forum, or Requests & Ideas (MQL5 only!).
  3. learn to code it. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
  4. or pay (Freelance) someone to code it.
We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help
          urgent help.
 

I read a lot about the documentations and made several attempts in the situation bellow, how can I record data of a csv file in a struct.

   #define MC_MAX 80

   struct line_read{
      char           symbol;  // symbol
      char            code1;   // field 2
      char           email;   // email
      char            code2;   // field 4
      char            code3;   // field 5
      bool           status;  // field 6
   };

   string           symbol[MC_MAX];  // symbol
   char            code1[MC_MAX];   // field 2
   string           email[MC_MAX];   // email
   char            code2[MC_MAX];   // field 4
   char            code3[MC_MAX];   // field 5
   bool           status[MC_MAX];  // field 6

   

   line_read      buff_line[200];
   string filename = "data.csv";
   int size=0;
   int handle=0;

   handle=FileOpen("data.csv", FILE_WRITE|FILE_READ|FILE_BIN,",");
   if(handle > 0){
      if(!FileReadStruct(handle,buff_line[0]))
      PrintFormat("Error reading data. Error code=%d",GetLastError());         
         
      Print("handle: ", handle," : ",buff_line[handle].email);
      FileFlush(handle);
      FileClose(handle);
   }
      
   Print("Struct Array: ",buff_line[1].email, " + ", buff_line[1].symbol , " + ", buff_line[1].code1);
   Print("Struct Array result: ",result[0]);

   

My CSV file format is:

EURUSD,1,sddw32@gmail.com,274653782,0,true

USDJPY,3,dffdff@gmail.com,274653782,0,false


When I try to read the file using FILE_CSV or FILE_TXT in function FileOpen() does'nt work and return error 5011 "File must be opened with FILE_BIN", but if I use this parameter I cant access the content of the struct in separate.

 
Leandro Moraes:

I need a function to read a file and save in one structure.

Stop trying to get people to do all the work for you.

Using the search function on mql5.com would bring up the answer for you.
https://www.mql5.com/en/search#!keyword=read%20csv&module=mql5_module_forum

This topic has been asked NUMEROUS times.

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...
 

Also if you would PLEASE read over the documentation, you will find your answer.

Here is the link to the documentation regarding file handling.

https://www.mql5.com/en/docs/files

Documentation on MQL5: File Functions
Documentation on MQL5: File Functions
  • www.mql5.com
For security reasons, work with files is strictly controlled in the MQL5 language. Files with which file operations are conducted using MQL5 means cannot be outside the file sandbox. the common folder for all the terminals installed on a computer - usually located in the directory C:\Documents and Settings\All Users\Application...
 
class SymbolDebug : public CObject
{
public:
string symbol; // symbol
int code1; // field 2
string email; // email
int code2; // field 4
int code3; // field 5
bool status; // field 6
bool load_string(string entire_csv_row)
{
string res[];
StringSplit(entire_csv_row, ',', res);
this.symbol = res[0];
this.code1 = int(res[1]);
//etc........
}
};
 

Thank to all, I solved with it:


      string line_read[10][100]; //assign array of string that will store 10 columns 100 rows of csv data
      int row=0,col=0; //column and row pointer for the array
      handle=FileOpen(filename,FILE_CSV|FILE_READ,","); //comma delimiter
      if(handle>0)
      {
        while(True) //loop through each cell
        {
          string temp = FileReadString(handle); //read csv cell
          if(FileIsEnding(handle)) break; //FileIsEnding = End of File
          line_read[col][row]=temp; //save reading result to array
          if(FileIsLineEnding(handle)) //FileIsLineEnding = End of Line
          {
            col = 0; //reset col = 0 for the next row
            row++; //next row
          }
          else
          {
            col++; //next col of the same row
          }
        }
        FileClose(handle);
      }
      else
      {
        Comment("File "+filename+" not found, the last error is ", GetLastError());
      }