FileWrite - does not add text into the end of file, but replaces the content of CSV file, why?

 

I need to add some information into the end of CSV or TXT file, but every time I open the file with FileOpen and write the text there, it erases all information and write a new text..

But I need to add the new information into the end of the file, while keeping the previous text as well. Kind of incremental approach.

I need to keep previous text after closing file with FileClose and opening again with FileOpen.

Any idea? How to get a file with increasing volume in every writing there?

Look forward to hearing from you.

Thanks.

Bahman

 
bahmanasgerovI need to add some information into the end of CSV or TXT file, but every time I open the file with FileOpen and write the text there, it erases all information and write a new text.. But I need to add the new information into the end of the file, while keeping the previous text as well. Kind of incremental approach. I need to keep previous text after closing file with FileClose and opening again with FileOpen. Any idea? How to get a file with increasing volume in every writing there? Look forward to hearing from you. Thanks. Bahman

File Opening Flags - Input/Output Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

One or several flags can be specified when opening a file. This is a combination of flags. The combination of flags is written using the sign of logical OR (|), which is positioned between enumerated flags. For example, to open a file in CSV format for reading and writing at the same time, specify the combination FILE_READ|FILE_WRITE|FILE_CSV.

Example:

   int filehandle=FileOpen(filename,FILE_READ|FILE_WRITE|FILE_CSV);

There are some specific features of work when you specify read and write flags:

  • If FILE_READ is specified, an attempt is made to open an existing file. If a file does not exist, file opening fails, a new file is not created.
  • FILE_READ|FILE_WRITE – a new file is created if the file with the specified name does not exist.
  • FILE_WRITE –  the file is created again with a zero size.
 

Is it the answer? I caannot get the answer here)), I need some option to use to open the file with addition, not replacement or deletion.

I use this code, that does not work...

int f=FileOpen("WriteString.csv",FILE_READ|FILE_WRITE|FILE_CSV);     FileWriteString(f,"1111111....\r\n");


Thanks for your kind answer.

Maybe Fernando Carreiro?

As usual, his answers are useful))

Fernando Carreiro
Fernando Carreiro
  • 2023.12.10
  • www.mql5.com
Trader's profile
 
bahmanasgerov #Is it the answer? I caannot get the answer here)), I need some option to use to open the file with addition, not replacement or deletion. I use this code, that does not work... int f=FileOpen("WriteString.csv",FILE_READ|FILE_WRITE|FILE_CSV);     FileWriteString(f,"1111111....\r\n"); [ . . . ]

Would you like a water or coffee while you wait?... Ask someone who knows how to read for help and see the article below... There's even the code ready:

Articles

MQL5 Programming Basics: Files

Dmitry Fedoseev, 2016.11.04 15:19

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.
See too:  File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vinicius Pereira De Oliveira #:

Would you like a water or coffee while you wait?... Ask someone who knows how to read for help and see the article below... There's even the code ready:

Thanks Viicius, I will have a look now. However, I have not transitioned to MT5 yet, I am still in MT4, let me see, if I can apply it for codes in MQL4.

Regards

Bahman

 
bahmanasgerov #Thanks Viicius, I will have a look now. However, I have not transitioned to MT5 yet, I am still in MT4, let me see, if I can apply it for codes in MQL4. Regards Bahman

OK, if the error persists, please post your code (using the CODE button - Alt+S) so we can identify why it isn't working.

 
Vinicius Pereira De Oliveira #:

OK, if the error persists, please post your code (using the CODE button - Alt+S) so we can identify why it isn't working.

dear Vinicicus, 


Thanks for your idea, it looks now I can understand your point of view. I mean if I include READ and WRITE options, then I can read the content of the file, 

save it into a string variable and then when I need to save the file, I can add it onto the beginning of newly added text and save them into a file together?

I hope, you meant this, it was a good idea, and thanks a lot for this. It looks my problem gets solved.

Regards

Bahman

 
bahmanasgerov #:

dear Vinicicus, 


Thanks for your idea, it looks now I can understand your point of view. I mean if I include READ and WRITE options, then I can read the content of the file, 

save it into a string variable and then when I need to save the file, I can add it onto the beginning of newly added text and save them into a file together?

I hope, you meant this, it was a good idea, and thanks a lot for this. It looks my problem gets solved.

Regards

Bahman

The following function I have made to add new text onto the end of the CSV file... Thanks a lot:

LineAdd(f,"1,2,3"); //function calling...

void LineAdd(int f,string s) {while(!FileIsEnding(f))FileReadString(f); FileWriteString(f,s+"\n");return;}



 
bahmanasgerov #The following code was enough to add new portion of text, onto the end of the CSV file... Thanks a lot: [ . . . ]

You're welcome, good luck!!