Writing when I want in *.txt file

 

Good night or good morning accordingt to where you are.

I am testing an EA I am coding and I use the print function to know what it is doing in order to debug it but it's appear to be heavy because there is news informations every ticks and the automatic refersh is a little boring even if I can disable it.

But I want also write importants informations once the EA wil be finish. So, I want now writing any informations (only string for the moment) into a txt file like.

==> I have copied the template for this site but I would have some help to write an info at the following of the other and in the example the number of infos are predeterminated.

How can I make to write an info into the text file without knowing the number of this infos?

//--------------------------------------------------------------------
// createfile.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------- 1 --
extern string Date_1="2007.05.11 10:30";
extern string Text_1="CHF Construction licenses";
extern string Date_2="2005.11 12:00";
extern string Text_2="GBP Refinance rate,2%,2.5%";
extern string Date_3="";   // 2007.05.11 13:15
extern string Text_3="";   // EUR Meeting of G10 banks governors
extern string Date_4="";   // 2007.05.11 15:30
extern string Text_4="";   // USD USA unemployment rate
extern string Date_5="";   // 2007.05.11 18:30
extern string Text_5="";   // JPY Industrial production
//--------------------------------------------------------------- 2 --
int start()                            // Spec. function start()
  {
//--------------------------------------------------------------- 3 --
   int Handle,                         // File descriptor
   Qnt_Symb;                           // Number of recorded symbols
   string File_Name="News.txt";        // File name
   string Erray[5,2];                  // Array for 5 news
//--------------------------------------------------------------- 4 --
   Erray[0,0]=Date_1;                  // Fill the array with values
   Erray[0,1]=Text_1;
   Erray[1,0]=Date_2;
   Erray[1,1]=Text_2;
   Erray[2,0]=Date_3;
   Erray[2,1]=Text_3;
   Erray[3,0]=Date_4;
   Erray[3,1]=Text_4;
   Erray[4,0]=Date_5;
   Erray[4,1]=Text_5;
//--------------------------------------------------------------- 5 --
   Handle=FileOpen(File_Name,FILE_CSV|FILE_WRITE,";");//File opening
   if(Handle==-1)                      // File opening fails
     {
      Alert("An error while opening the file. ",// Error message
              "May be the file is busy by the other applictiom");
      PlaySound("boo.wav");          // Sound accompaniment
      return;                          // Exir start()      
     }
//--------------------------------------------------------------- 6 --
   for(int i=0; i<=4; i++)             // Cycle throughout the array
     {
      if(StringLen(Erray[i,0])== 0  || // If the value of the first or..
         StringLen(Erray[i,1])== 0)    // ..second variable is not entered
         break;                        // .. then exit the cycle
      Qnt_Symb=FileWrite(Handle,Erray[i,0],Erray[i,1]);//Writing to the file
      if(Qnt_Symb < 0)                 // If failed
        {
         Alert("Error writing to the file",GetLastError());// Message
         PlaySound("boo.wav");       // Sound accompaniment
         FileClose( Handle );          // File closing
         return;                       // Exit start()      
        }
     }
//--------------------------------------------------------------- 7 --
   FileClose( Handle );                // File closing
   Alert("The ",File_Name," file created.");// Message
   PlaySound("ok.wav");              // Sound accompaniment
   return;                             // Exit start()
  }
//--------------------------------------------------------------- 8 --
 
emmett_brown:

How can I make to write an info into the text file without knowing the number of this infos?

You will be using something like the FileWrite() function. You can use this as many times as you like and more and more things can get written to the file until you close it using the FileClose() command. Once you look up the help on FileWrite() all the other available commands are conveniently grouped with it.
 

Thanks you, I had not looked the good function, but it still a question; when I create a CSV file, there is ";" between each datas and line, it is normal for the excel program will reconize it

But I just want a text file, so I use FILE_BIN and before the carriage return, there is a caracter like a 0 or O, how can I make to delete it or to not write it?

See the attached file to know what I mean...

int start()
  {
//----
   int handle;
   datetime date = Time[0];
   string str="some string";
   handle=FileOpen("filename.txt", FILE_BIN|FILE_WRITE);
     if(handle<1)
     {
      Print("can\'t open file error-",GetLastError());
      return(0);
     }
   for (int i=0; i<=5; i++)  {
      FileWrite(handle, TimeToStr(date), "Ligne n° ", i, str, "\r\n");  
   }
   //FileClose(handle);
//----
   return(0);
  }
Files:
filename.txt  1 kb
 
emmett_brown:

Thanks you, I had not looked the good function, but it still a question; when I create a CSV file, there is ";" between each datas and line, it is normal for the excel program will reconize it

But I just want a text file, so I use FILE_BIN and before the carriage return, there is a caracter like a 0 or O, how can I make to delete it or to not write it?

Like this ...

int start(){

   int handle;
   datetime date = Time[0];
   string str="some string";
   
   handle=FileOpen("filename.txt", FILE_WRITE, " ");

   if( handle<1 ){
      Print("can\'t open file error-",GetLastError());
      return(0);
   }
   
   for( int i=0; i<=5; i++ ){
      FileWrite(handle, TimeToStr(date), "Ligne n° ", i, str);  
   }
   
   FileClose(handle);

   return(0);
}