Can't add data to a file using FileSeek fucntion

 

Hello, community,

I'm trying to create an algo that, at every new tick, it will add some data in a .txt file. To achieve that, I'm using the FileSeek function following the instructions available in the "Writing to the end of a text file" Section in https://www.mql5.com/en/articles/2720#z3.

However, the algo is not adding the new information to the file. On the contrary, it is overwriting the existing data, so the file always contains one line of data. Thus, I do not get any noticeable errors in the execution.


Can anyone enlighten me on how to solve it?


This is the algo I wrote

// Global variables
int fileHandle = 0; // file handler

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   //---
   MqlTick lastTick;
   SymbolInfoTick(_Symbol, lastTick);
   open_file();
   write_file(lastTick.time, lastTick.time_msc, lastTick.bid, lastTick.ask);
   close_file();   
  }

///////////////////////////////////////////////////////////////////////   
//                         FILE HANDLER MODEL                        //
///////////////////////////////////////////////////////////////////////
void open_file()
  {
   // Set file name
   string fileName = "tick-reg_0001.txt";                 
   //--- write data
   Print(fileName);
   fileHandle = FileOpen(fileName,
                         FILE_READ|FILE_WRITE|FILE_ANSI|FILE_TXT,
                         '\t');

   if(fileHandle != INVALID_HANDLE)
     {
      Print("Opening file...");
      // Position pointer in the EOF
      FileSeek(fileHandle, 0, SEEK_END);   
     }
   else
     {
      Print("error ", GetLastError());
      return;
     }   
  }


void write_file(datetime timestamp, long time_msc, double bid, double ask) 
  {      
   //---
   FileWrite(fileHandle,
             TimeToString(timestamp),
             time_msc,
             DoubleToString(bid, _Digits),
             DoubleToString(ask, _Digits));
  }


void close_file()
  {
   FileClose(fileHandle);
   Print("File Closed");   
  }


txt file output:

2022.08.19 11:46        3362034172839   1.62014      1.62040


Expert log


Thanks!

MQL5 Programming Basics: Files
MQL5 Programming Basics: Files
  • www.mql5.com
This practice-oriented article focuses on working with files in MQL5. It offers a number of simple tasks allowing you to grasp the basics and hone your skills.
 
Otavio Pereira:

Hello, community,

I'm trying to create an algo that, at every new tick, it will add some data in a .txt file. To achieve that, I'm using the FileSeek function following the instructions available in the "Writing to the end of a text file" Section in https://www.mql5.com/en/articles/2720#z3.

However, the algo is not adding the new information to the file. On the contrary, it is overwriting the existing data, so the file always contains one line of data. Thus, I do not get any noticeable errors in the execution.


Can anyone enlighten me on how to solve it?


This is the algo I wrote


txt file output:


Expert log


Thanks!

I ran your code (in MQL5) - worked fine:

2022.08.19 15:23 1660922610017 1.18252 1.18265

2022.08.19 15:24 1660922654877 1.18257 1.18271

2022.08.19 15:24 1660922660515 1.18266 1.18276

2022.08.19 15:24 1660922662316 1.18268 1.18280

2022.08.19 15:24 1660922663292 1.18261 1.18271

2022.08.19 15:29 1660922989902 1.18258 1.18270

2022.08.19 15:29 1660922994056 1.18250 1.18261


I had a look at some code I wrote a while back and I had done it differently, without the delimiter and using FileWriteString() which worked fine.


Maybe try that:

   filehandle = FileOpen(iv_fileName, FILE_READ | FILE_WRITE | FILE_TXT);

   if(filehandle == INVALID_HANDLE)
     {
      return(false);
     }
   else
     {
      FileSeek(filehandle, 0, SEEK_END); //Append to file
      ov_bytesWritten = (int)FileWriteString(filehandle, line);
      FileClose(filehandle);
//etc
 
R4tna C #:

I ran your code (in MQL5) - worked fine:

2022.08.19 15:23 1660922610017 1.18252 1.18265

2022.08.19 15:24 1660922654877 1.18257 1.18271

2022.08.19 15:24 1660922660515 1.18266 1.18276

2022.08.19 15:24 1660922662316 1.18268 1.18280

2022.08.19 15:24 1660922663292 1.18261 1.18271

2022.08.19 15:29 1660922989902 1.18258 1.18270

2022.08.19 15:29 1660922994056 1.18250 1.18261


I had a look at some code I wrote a while back and I had done it differently, without the delimiter and using FileWriteString() which worked fine.


Maybe try that:

Thanks for your reply! It's good to know that the algo is working.

For some reason, the data is being overwriten. And today I got another not intended result. For some time, the algo was not able to write the data in the file. None. I checked if the txt file was open or for an other reason, but could not find any.

I'm using MT5, v 5, build 3391.

I also tried your sugestion, but could not get any output.

Thanks for your help.

 
Otavio Pereira #:

Thanks for your reply! It's good to know that the algo is working.

For some reason, the data is being overwriten. And today I got another not intended result. For some time, the algo was not able to write the data in the file. None. I checked if the txt file was open or for an other reason, but could not find any.

I'm using MT5, v 5, build 3391.

I also tried your sugestion, but could not get any output.

Thanks for your help.

Same build as me. Not sure why you have the problem.

I did wonder why you had 3 separate functions for open, write & close. Are you keeping the file unclosed for long periods?

I suggest combining the open and close operations into a single function write_file() so everything is done together 

Also FileSeek and FileWrite have return values - print those out and it may reveal where the problem is
 

I could solve the problem by adding FILE_COMMON in the FileOpen function.

   //Print(fileName);
   fileHandle = FileOpen(fileName,
                         FILE_READ|FILE_COMMON|FILE_WRITE|FILE_ANSI|FILE_TXT,
                         '\t');

Thanks for the help!

Cheers!

Reason: