printf() / PrintFormat() to specified file, or Write to specified file with printf/PrintFormat() format strings...?

 

Hello all.

I've read the documentation, as best as I can find.  If the answers to the following are in the documentation, please do feel free to just point me there.

As far as I can tell: 

  • In my EA, I can use printf() or PrintFormat() (https://www.mql5.com/en/docs/common/printformat) I can use most, if not all, of the standard C format strings to format the output.  But I can't see any way to specify a file (off my choosing) to write that to.  It writes it all to the Experts tab as well as into a yyyymmdd.log file in the default logs folder.
  • Alternatively, I can use FileWrite() (https://www.mql5.com/en/docs/files/filewrite, and all the other file functions at https://www.mql5.com/en/docs/files), I can specify a file of my choosing (or multiple files if I want) and write text to there, but I can't see any way to use the format strings available to me with printf()/PrintFormat().

Is there something I'm missing here or is it not possible to use format strings to write to a file of my choosing?


Secondly, with printf()/PrintFormat() to the dated file in the default logs folder, I'm finding sometimes it just doesn't write anything to the log file (while still writing to the Experts tab).  I'm wondering if this is because I need to do some kind of flush (like FileFlush() but specifically for the default dated .log file), but if that's possible, I can't see any function that can do that (FileFlush() appears to only work with the same handles all the other File... functions use, but can't apply it to the default dated .log file, as far as I can tell).


Thanks for any help on any of this that anyone can provide.

Documentation on MQL5: Common Functions / PrintFormat
Documentation on MQL5: Common Functions / PrintFormat
  • www.mql5.com
PrintFormat - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Detnator:
...but I can't see any way to use the format strings available to me with printf()/PrintFormat().
    string line = StringFormat("What?  %d ",42);
     
    Detnator:

    Secondly, ...

    I have never had such a problem. Maybe it's because I use my own files (FileOpen, FileWrite)  instead of the log file when I do time-critical things.

     
    The terminal log files are cached. They will only be flushed if you open them from within the terminal. (Right-click open) or when the terminal gets closed.

    You cannot flush them to disk from within a program.
     

    Willbur #:

    string line = StringFormat("What?  %d ",42);


    Ah... seems that will do it. I find most things I'm looking for...  Checked all the String functions.  Didn't think to check Conversion functions.  Thank you!


    Willbur #:

    I have never had such a problem. Maybe it's because I use my own files (FileOpen, FileWrite)  instead of the log file when I do time-critical things.

    Indeed, makes sense -- those files you can flush.  And as confirmed by Dominik in the reply after yours, it seems you and I have both guessed the problem correctly (with the default log files).  And also confirmed by him, we can't solve it programmatically, which is what led me to seek the formatting options for using my own files. So... thank you again for pointing me there. 😊 

     
    Dominik Christian Egert #:
    The terminal log files are cached. They will only be flushed if you open them from within the terminal. (Right-click open) or when the terminal gets closed.

    You cannot flush them to disk from within a program.

    Seems I sorta guessed the problem correctly. And I suppose I guessed the solution (or rather the lack of it, for the default log files) correctly too. And Wilbur has given me the means to get the desired result.

    Thank you for confirming/clarifying! 😊