mql5 write in file

 

Hi . I write this code in mql4 and run in mt4 and works good . Means if i drop this indicator in 7 chart , then 7 symbols write in the file.

But when i write in mql5 and run in mt5 , when i drop this indicator in 7 chart i see in the file just 2 symbol by random.

Is it a bug in mql5 ?

   string Handle2;

   Handle2 =FileOpen("test.txt",FILE_TXT|FILE_READ|FILE_WRITE,"\r\n");

   FileSeek(Handle2, 0,SEEK_END);

   FileWriteString(Handle2,Symbol()+ "\r\n");

   FileClose(Handle2);
 
mir202020:

Hi . I write this code in mql4 and run in mt4 and works good . Means if i drop this indicator in 7 chart , then 7 symbols write in the file.

But when i write in mql5 and run in mt5 , when i drop this indicator in 7 chart i see in the file just 2 symbol by random.

Is it a bug in mql5 ?

   string Handle2;

   Handle2 =FileOpen("test.txt",FILE_TXT|FILE_READ|FILE_WRITE,"\r\n");

   FileSeek(Handle2, 0,SEEK_END);

   FileWriteString(Handle2,Symbol()+ "\r\n");

   FileClose(Handle2);

Please read carefully the documentation https://www.mql5.com/en/docs/files

Especially check FileOpen: the type for the delimiter is short (not string); and the return type is int, not string.

Insert

#property strict

at the beginnig of your code.

Matthias

Documentation on MQL5: File Functions
Documentation on MQL5: File Functions
  • www.mql5.com
File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. Handle2 =FileOpen("test.txt",FILE_TXT|FILE_READ|FILE_WRITE,"\r\n");
    When another thread has the file open, this fails. You don't handle failures. Delay and retry.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

 
Dr Matthias Hammelsbeck:

Please read carefully the documentation https://www.mql5.com/en/docs/files

Especially check FileOpen: the type for the delimiter is short (not string); and the return type is int, not string.

Insert

at the beginnig of your code.

Matthias

I changed Handle2 to int and add property strict. but does not work. 
thank you.

Can you Drop this indicator in mt5 int 7 chart and see the result? In mt4 code works.
 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. When another thread has the file open, this fails. You don't handle failures. Delay and retry.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

Why this code works in mt4 ? 
thank you anyway.
 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. When another thread has the file open, this fails. You don't handle failures. Delay and retry.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

I added this code to end of code because of last thread.
Sleep(5000);

But does not works. How can say that run multithread?

 
Dr Matthias Hammelsbeck:

Please read carefully the documentation https://www.mql5.com/en/docs/files

Especially check FileOpen: the type for the delimiter is short (not string); and the return type is int, not string.

Insert

at the beginnig of your code.

Matthias

#property strict is useless with mql5.
 
  1. mir202020: But does not works. 

    Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button).
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

    We can't see your broken code. You added to the end of code? What part of “retry” was unclear?

  2. You were asked to edit your (original) post. Why didn't you?

 

I write code by script instead indicator because of thread.

It works good . thank you.

 

Indeed we only have broken code without the environment. Difficult problem...

A proposition for an (rudimentary) indicator:

#property strict
#property indicator_chart_window

int OnInit() {
   int Handle2;
   Handle2 =FileOpen("test.txt",FILE_TXT|FILE_READ|FILE_WRITE|FILE_ANSI);
   FileSeek(Handle2, 0,SEEK_END);
   FileWriteString(Handle2,Symbol() + "\r\n");
   FileClose(Handle2);
   return(INIT_SUCCEEDED);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[]) {
   return(rates_total);
}

Please note the FILE_ANSI flag in call of FileOpen.

It works fine without any problems here :-)


Matthias

 
Dr Matthias Hammelsbeck:

Indeed we only have broken code without the environment. Difficult problem...

A proposition for an (rudimentary) indicator:

Please note the FILE_ANSI flag in call of FileOpen.

It works fine without any problems here :-)


Matthias

Is your code writen in mql5 ? or mql4?

Reason: