Write array to CSV file issue - page 2

 
Robin:

Marco,


That is kind of the intention, I just don't know how. The data must be stored in a file as it gets used to make trading decisions and gets updated after each new trade, power failure, system closing down, whaterver wont be an issue.

The file is only 5 lines x 25 numbers max (all double) It might increase to 10 line, but no more.

Each line needs to be processed separately, I increment one number from each line (see calculation in code above) then I replace the whole line. (this is a score, one of the 4 numbers gets increased or decreased after analysing the last order closed) Then I go to the next line and do the same for the relevant feature, in the example above its GetQuad().

There are two issues, the first relates to my question above, I get output one number per line, instead of all 4 numbers on the same line separated by  a comma.

Sounds simple enough, but every solution in one part seems to introduce another issue.

Reading the file is no problem.

You just need to hack around a bit until you got it right and prepare the line to be written.

I noticed you use a string array but you will probably use double array and use DoubleToString() Function.

I don't understand exactly what your trying to do but you can write whole lines too.

Here is an example.

//+------------------------------------------------------------------+
//|                                            CSV Write Example.mq5 |
//|       Copyright 2020,Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020,Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   string var_array[]= {"10","78.29","10","12.57"};
   string line = "";

   for(int i=0; i<ArraySize(var_array); i++)
      {
         line += var_array[i] + ",";
      }

   Print(line);                                   // check result
   line = StringSubstr(line,0,StringLen(line)-1); // strip last delimiter
   Print(line);                                   // check result
   line += "\n";                                  // add newline chr
   uint res = AppendToFile("test.csv",line);      // append to file
   Print((string)res+" Bytes Written");           // print result
  }
//+-----------------------------------------------------------------------+
//| AppendToFile() Function
//| The function will append to the file, on the same line until you pass
//| a "\n" new line chr.
//|
//| Example: uint bytes = AppendToFile("test.txt","Hello this is a test");
//| Alert("Bytes Writte: "+(string)bytes);
//+-----------------------------------------------------------------------+
uint AppendToFile(string filename,string text)
{
   uint bytes_written = -1;
   int  filehandle    = FileOpen(filename,FILE_READ|FILE_WRITE);
   if(filehandle == INVALID_HANDLE)
      {
         // Something went wrong here.
         return(-1);
      }
   bool seek = FileSeek(filehandle,0,SEEK_END);
   if(seek == false)
      {
         // Something went wrong here.
         return(-1);
      }
   if(seek == true)
      {
         bytes_written = FileWriteString(filehandle, text, StringLen(text));
         // The FileWriteString() function writes the value of a string-type parameter
         // into a BIN, CSV or TXT file starting from the current position of the file pointer.
         // When writing to a CSV or TXT file:
         // if there is a symbol in the string '\n' (LF) without previous character '\r' (CR),
         // then before '\n' the missing '\r' is added.
      }
   FileClose(filehandle);
   return(bytes_written);
}
//+------------------------------------------------------------------+


Reason: