I can't write my file!!!!

Back to topics list To post a new topic, please log in or register
avatar
48
landmeister 2016.06.28 00:12
 

Dear all,

I created my first code for creating and writinf a file using mql4. I want to be sure it works properly so I decided to write some welcome words every time I execute it. Sadly, the file is correctly created but nothing is written in it. I attach my code for the community to take a humble look. I guess it must be a dumb error, but I simply can't see it. Here it is (it's in Spanish).

int OnInit()
  {

   str=StringConcatenate("ea_cierre_parcial",TimeToStr(TimeCurrent(),TIME_DATE),".log");
  
   Escritura_Fichero(StringConcatenate("Empieza el log ",str));

   }

void Escritura_Fichero(string cadena_entrada)
   {
   bool log_creado,cambio_por_tamanyo;
   string entrada_log;
   
   fichero=FileOpen(str,FILE_BIN|FILE_READ|FILE_WRITE);
   if (fichero<1)
      {
      Alert("No se ha podido abrir el fichero de log. Error=",GetLastError());
      log_creado=false;
      }
   else
      {
      cambio_por_tamanyo=Comprobacion_Log_Tamanyo();
      if (cambio_por_tamanyo==true)
         {
         FileClose(fichero);
         fichero=FileOpen(str,FILE_BIN|FILE_READ|FILE_WRITE);
         if (fichero<1)
            {
            Alert("No se ha podido abrir el fichero de log. Error=",GetLastError());
            log_creado=false;
            }
         else
            {
            log_creado=true;
            }
         }
      else
         {
         log_creado=true;
         }
      }
   if (log_creado==true)
      {
      entrada_log=StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS),"->",cadena_entrada,"\r\n");
      FileSeek(fichero,0,SEEK_END);
      FileWrite(fichero,entrada_log);
      }
   FileClose(fichero);
   }

Thank you very much in advance.

avatar
48
landmeister 2016.06.28 08:32 #
 
Is it correct?
avatar
1197
FMIC 2016.06.28 09:24 #
 

You are opening a BINARY file and trying to output text in CSV format:

fichero=FileOpen(str,FILE_BIN|FILE_READ|FILE_WRITE); // Open Binary File
...
FileWrite(fichero,entrada_log); // Write in CSV/Text format

Open a CSV or Text file instead, using FILE_CSV or FILE_TXT (either in ANSI or UNICODE).

avatar
164
drazen64 2016.06.28 10:00 #
 

Please read documentation on FileOpen() function, especially example code.

There are examples of "incorrect" and "correct" file opening code.

You use "incorrect" one and besides that looks like you have a bug.

When fails, FileOpen() returns INVALID_HANDLE, which is -1, so your code probably correctly opens file (since you can see it in the folder), but you incorrectly conclude that FileOpen() failed. 

 

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- incorrect file opening method
   string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
   string filename=terminal_data_path+"\\MQL4\\Files\\"+"fractals.csv";
   int filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV);
   if(filehandle<0)
     {
      Print("Failed to open the file by the absolute path ");
      Print("Error code ",GetLastError());
     }
//--- correct way of working in the "file sandbox"
   ResetLastError();
   filehandle=FileOpen("fractals.csv",FILE_WRITE|FILE_CSV);
   if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(ENUM_TIMEFRAMES(_Period)));
      FileClose(filehandle);
      Print("FileOpen OK");
     }
   else Print("Operation FileOpen failed, error ",GetLastError());
//--- another example with the creation of an enclosed directory in MQL4\Files\
   string subfolder="Research";
   filehandle=FileOpen(subfolder+"\\fractals.txt",FILE_WRITE|FILE_CSV);
      if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(ENUM_TIMEFRAMES(_Period)));
      FileClose(filehandle);
      Print("The file most be created in the folder "+terminal_data_path+"\\"+subfolder);
     }
   else Print("File open failed, error ",GetLastError());
  }

 

avatar
48
landmeister 2016.06.29 00:19 #
 

Hi,

I've changed my code:

...
fichero=FileOpen(str,FILE_TXT|FILE_WRITE);
...
if (fichero==INVALID_HANDLE)
...

and now is working properly.

Thank you very much both for your help.

Best Regards,

Back to topics list  

To add comments, please log in or register