csvmql4metatrader4

How to read specific cell value from CSV file in MQL4?


I am trying to read just one cell value from a CSV file in MQL4, however, the online documentation doesn't cover finding single cell values and I've searched countless threads for a workaround but there are none.

My code is below,

  int Handle = FileOpen("/Users/rahulparmeshwar/Documents/Algo 
  /LogisticRegressionOutput.csv",FILE_READ|FILE_CSV,',');
  double Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
  Str = FileReadInteger(Handle,1);
 Alert(Str);
 FileClose(Handle);

The contents of the CSV file are as follows below Data

Any help will be much appreciated, Thank you.


Solution

  • First, see FileOpen Function

    For security reasons, work with files is strictly controlled in the MQL4 language. Files with which file operations are conducted using MQL4 means, cannot be outside the file sandbox.

    This means you must place your csv file in the ...\MQL4\Files folder.

    Regarding your code, it is much more efficient to use an array. You can then read a specific cell value using [row][column]. Remember that arrays are zero based (your first row and column reference would be [0][0]).

    string data[1][18]
    
    int Handle=FileOpen("LogisticRegressionOutput.csv",FILE_CSV|FILE_SHARE_READ,",");
    if(Handle<1){return(-1);}
    int row=0;
    ArrayResize(data,18);
    while(!FileIsEnding(Handle))
    {
       if(row>ArraySize(data)/18-1) {ArrayResize(data,(row+1)*18);}
       for(int i=0; i<=18-1; i++) data[row][i]=FileReadString(Handle);
       row++;
    }
    FileClose(Handle);
    Print(data[0][0]);