Error when Creating and Writing to a .txt file

 
Hello,
I can't locate the location of the created file.
Can someone guide me?

Thanks in advance,

Juan Luis

string FileName="NuevoTXT "+TimeToString(TimeCurrent(),TIME_DATE)+".txt";

int OnInit()
{
   TXTCrear();
   TXTNuevaLinea();
   TXTNuevaLinea();
Print("Fin "+ FileName);
   return(INIT_SUCCEEDED);
}

void TXTCrear()
{     
   int h=FileOpen(FileName,FILE_READ|FILE_WRITE|FILE_ANSI|FILE_TXT);
   if(h==INVALID_HANDLE){Print("Error al crear el archivo: "+FileName);return;}

   FileSeek(h,0,SEEK_END);
   FileWrite(h,TimeToString(TimeCurrent(),TIME_MINUTES)+" Inicio el archivo txt.");
   FileClose(h);
} 
 
 
void TXTNuevaLinea()
{     
   int h=FileOpen(FileName,FILE_READ|FILE_WRITE|FILE_ANSI|FILE_TXT);
   if(h==INVALID_HANDLE){Print("Error al abrir el archivo: "+FileName);return;}
   
   FileSeek(h,0,SEEK_END);
   FileWrite(h,"Nueva Linea "+TimeToString(TimeCurrent(),TIME_MINUTES));
   FileClose(h);
}  



 
  1. febrero59: I can't locate the location of the created file.

    File (Alt+F) → Open Data Folder and look in Files

  2. string FileName="NuevoTXT "+TimeToString(TimeCurrent(),TIME_DATE)+".txt";
    
    int OnInit()
    {
       TXTCrear();
       TXTNuevaLinea();
       TXTNuevaLinea();

    That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

 
Understood William, thanks.

Is there an easy way to assign another path?. Although I already know that it must be within the mql5 directory environment.

Thank you very much.
 
febrero59 #:
Understood William, thanks.

Is there an easy way to assign another path?. Although I already know that it must be within the mql5 directory environment.

Thank you very much.

Place the text cursor above FileOpen and press F1 then read about the Flag FILE_COMMON.

 
Thanks Carl. 
 
The FileOpen code example is also a good starting point to see what works and what doesn't.
Reason: