Search code examples
mql4

Cannot open file on Ubuntu


I'm using Ubuntu 14 and trying to create a script to write files, but I'm getting the 5004 error, every time I try to open a file.

datetime currtime;
bool     newcandle;

string   terminal_data_path = TerminalInfoString( TERMINAL_DATA_PATH );
string   filename           = terminal_data_path + "\\MQL4\\Files\\" + "data.csv";

int      filehandle;

filehandle = FileOpen( filename, FILE_WRITE | FILE_CSV );

if (  filehandle < 0 ){    
      Print( "Failed to open the file by the absolute path " );
      Print( "Error code ", GetLastError() );
    }
else {
      Print( "file opened with sucess" );
    } 

How can I solve this problem on Ubuntu?

UPDATE

I tried to change my file to the following:

string terminal_data_path = TerminalInfoString( TERMINAL_DATA_PATH );
string filename           = terminal_data_path + "\\tester\\files\\data.csv";

and just for this

string filename = "\\tester\\files\\data.csv";

and for this

string filename = "\\files\\data.csv";

But I'm still getting error, but this time is 5002 not 5004.


Solution

  • MQL4 Permissions By Design Do Not Allow / Restrict FileIO

    There are three directories (with subdirectories) where working files can be placed:

    /HISTORY/<current broker> - especially for the FileOpenHistory() function;

    /EXPERTS/FILES - common case;

    /TESTER/FILES - especially for testing ( ref. during Strategy Tester operations ).

    Working with files from other directories is prohibited.

    Solution

    Adapt your MQL4-code so as to meet this fact and respect pre-Build 762 and post-Build 762 differences ( a "new"-MQL4 file-localisations ).

    Update

    As posted, your MQL4-code ( whether you share it's updated state or not ) shall better handle exceptions. Have met several suprising artefacts with filenames. Some platform specific, causing no harm in wXP, but failing to operate (the same code) on VPS-hosted wServer2008 VM or a LinuxVM-encapsulated Wine/MT4 instance.

    Carefully read MQL4-help documentation and create a few post-mortem tools to step further.

    5002
     ERR_FILE_WRONG_FILENAME
     Wrong file name               -------> pre-test + "fuse" the corner cases
    
    5003
     ERR_FILE_TOO_LONG_FILENAME
     Too long file name
    
    5004                           <------ a good sign, we are on the safer side here
     ERR_FILE_CANNOT_OPEN
     Cannot open file
    
    //-------------------------------------------------------------
    //   MT4_GUI_postMortem
    //-------------------------------------------------------------    
    void MT4_GUI_postMortem( string aFileNAME = "caller forgot to pass aFileNAME"
                             ){
      // SYNTAX
      //         if ( aFileHANDLE == INVALID_HANDLE ) MT4_GUI_postMortem( filename );
      //
         int aLastErrorNUM = GetLastError();
         Comment( "POST-MORTEM >>> [", aFileNAME, "] Threw error ", aLastErrorNUM );
         Print(   "POST-MORTEM >>> [", aFileNAME, "] Threw error ", aLastErrorNUM );
    
         return;
        }