You need file read and file write. file write will just overwrite the existing contents so you need to read in too...
FileOpen(file_name,FILE_CSV|FILE_READ|FILE_WRITE,';');
hth
V
It reads the whole file in, seeks the end, appends your line and then writes the whole lot back overwriting the current contents of the file. If you haven't read in anything, there is no end to seek so your line gets written as the first line and then that then overwirtes the current file contents...it doesn't really append to the file, it appends to a temp dataset and then overwites the file.
V
edit: whether that is what technically happens, I'm not sure, but descriptively, that's how I understand the process.
If FILE_WRITE does not combine with FILE_READ, a zero-length file will be opened. If even the file containd some data, they will be deleted. If there is a need to add data to an existing file, it must be opened using combination of FILE_READ | FILE_WRITE.
Hi all, I still got the problem that all my existing data/lines are overwritten.
I am using the creation of a CSV file to export some numbers during testing, every time when a test has ended a new line with the results should be appended. But it does not work.
Can someone help me, what am I doing wrong. Thank you.
bool WriteFile() { bool toReturn = true; //File string fName = "TestResults.csv"; //Open or creates file int fInt = FileOpen(fName, FILE_CSV|FILE_READ|FILE_WRITE|FILE_ANSI,";"); if(fInt!=INVALID_HANDLE) { //Go to the end of the file FileSeek(fInt, 0, SEEK_END); //Write to file if(!FileWrite(fInt,i_Tipo,i_Period,i_Gap,i_Close,i_MinGap,T,t_p,t_n,U,u_p,u_n,N,n_p,n_n)) { toReturn=false; } } //Close the file FileClose(fInt); return(toReturn); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
hi,
i am writing up a log file from an EA. if the EA is restarted i want it to carry on writing to the same file... but append to the end of the file. initially this did not work for me, so i searched the forum and found https://forum.mql4.com/13774. i applied the directions there and my init() function now looks like:
when the code starts the file descriptor is shifted to the end (i get "appending to file" in the experts log, so i gather that FileSeek() was successful). but the original file is overwritten. it is quite infuriating that something so simple is presenting such an obstacle! please can somebody tell me what (silly thing) i am doing wrong? thanks!
best regards, andrew.