1
votes

I am trying to create a textfile with MQL4. No sucess. It just doesn't work. A very simple script:

    void OnStart() {
      string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
      string filename=terminal_data_path+"\\MQL4\\Files\\"+"teste2.txt";
      int filehandle = FileOpen(filename,FILE_WRITE|FILE_TXT);
      FileWriteString(filehandle,"teste");  
      FileClose(filehandle); 
   }

This fires error 5002. OK, the file does not exist. I thinked that the script will create the file.

So, I decided to create an empity "teste2.txt" with notepad in the folder. The same error.

Can someone help me?

Thanks

3

3 Answers

1
votes

The file is written by default in .../MQL4/Files, so just writting that code works (it creates a file named teste2.txt with teste written in it in .../MQL4/Files) :

void OnStart()
{
  int filehandle = FileOpen("teste2.txt",FILE_WRITE|FILE_TXT);
  FileWriteString(filehandle,"teste");  
  FileClose(filehandle); 
}

Of course you will need to check the return of the FileX functions (FileOpen, fileWrite, FileClose, etc)

0
votes

if you call your file string filename="A"+"\\B\\"+"teste2.txt"; it will be written into TerminalInfoString(TERMINAL_DATA_PATH)\MQL4\Files\A\B\ folders. Of course you cannot use prohibited symbols in the file name, and ":\" from the full path (C:\Users\User_NAME\AppData...) is prohibited

0
votes

First, you need to check to see if there's a file.

//+------------------------------------------------------------------+
//|                                                  FileIsExist.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
     string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
     string filename     = "teste2.txt";
     int fileHandle      ;    

     if(FileIsExist(filename,0))
       {
          Print("Specified File Has");
          fileHandle     =    FileOpen(filename , FILE_WRITE|FILE_TXT);
          FileWriteString(fileHandle,"teste");  
          FileClose(fileHandle); 
          Print("Write to Existing File Completed");

       }else
          {
               Print("File Not Available, Regenerating....." );
               fileHandle     =    FileOpen(filename , FILE_READ|FILE_WRITE|FILE_TXT);
               FileWriteString(fileHandle,"Writing to Newly Created File Completed - teste     \n");  
               FileClose(fileHandle); 
               Print("Writing to Newly Created File Completed");
          }

  }
//+------------------------------------------------------------------+