How to use CFileTxt

 

The following code always returns -1.

#include <Files\FileTxt.mqh>


CFileTxt oF ;
string sFileName = "c:\tmp\FXReport.txt" ;
void OnStart()
  {
//---
    if ( oF.IsExist ( sFileName ) )
    {
      Print ( "File Exits" ) ;

      oF.Open ( sFileName, FILE_REWRITE ) ;
    }
    else
    {
      Print ( "RetCode: ", oF.Open ( sFileName, FILE_WRITE | FILE_CSV) ) ;
    }
    oF.WriteString ( "This,is,a,test" ) ;
    oF.WriteString ( "1This,2is,3a,4test" ) ;
    oF.Save (oF.Handle ( ) ) ;
    oF.Close (  ) ;
  }
 
Documentation on MQL5: Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
  • www.mql5.com
Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags - Documentation on MQL5
 
See section File Functions:

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.

There are two directories (with subdirectories) in which working files can be located:

  • terminal_path\MQL5\FILES\ (in the terminal menu select to view "File" - "Open the data directory");
  • the common folder for all the terminals installed on a computer - usually located in the directory C:\Documents and Settings\All Users\Application Data\MetaQuotes\Terminal\Common\.