File handling problems

 

Hello. I've written some code using FileOpen(), FileWrite(), FileFlush() and FileClose(). However, they don't seem to be working the way I expect them to.


First of all I have an EA I've called TickLogger. It is supposed to log ticks. I've added this code. It is supposed to update two files: a "tick" file and an "M1" file. Every time a new M1 bar opens, it is supposed to run FileFlush() on both file handles. I would have thought this would flush the buffer to the disk for both files but this does not consistently happen. It seems that the only time I can semi-reliably expect this to happen is when I close my EA and the FileClose() function is run.


Furthermore, sometimes I lose large amounts of data. An example of this is the following code:


/* logTrade() */
bool logTrade(int thisPeriod, int magicNumber, int operation, string programme, string release, int ticket) {
string symbol = Symbol();
bool returnCode = 1;
int handle = FileOpen(("TradeLog_" + symbol + "_Per" + thisPeriod + "_" + release + "_" + magic + ".csv"), FILE_CSV|FILE_READ|FILE_WRITE, ',');

if(handle < 1) {
Print("Problem opening Tradelog: ", GetLastError());
returnCode = 0;
}

if (FileWrite(handle, AccountNumber(), symbol, magicNumber, operation, programme, release, TimeCurrent(), ticket, Bid, Ask) < 0) {
Print("Error writing to Tradelog file: " + GetLastError());
returnCode = 0;
}

FileClose(handle);
return (returnCode);
} // bool logTrade()


This code just seems to overwrite the file every time the function is run. It doesn't append. I thought "FILE_CSV|FILE_READ|FILE_WRITE" was supposed to create a file if it doesn't exist, and append if the file is already there. This seems to be working for my TickLogger, but not for this logTrade() function.


If anyone can show me what I'm doing wrong, I would very much appreciate it. Thanks!

Files:
 

i got this in my generic fileopen function as comment - i never remember anything so make blurb which at time i 'think' i understand - lol

maybe ans u question... ie, gotta 'seek' to EOF

that why on open u nuke file cuz where else file open? gotta start at file head, then u can then tell filesys where u wanna go via seek function

oh yes, not make diff if file empty and issue seek to EOF, just setting file ptr in filesys, not hurt file etc...

hth

/*

NOTICE how Read|Write modes used to ensure file not zeroised IF exists so can fileseek to end... (ref:***always*** below)

[write to new [CSV|BIN] file] for CSV file iMode actual not reqd as function DEFAULT iMode=FILE_CSV|FILE_WRITE
for BINary file actual must be iMode=FILE_BIN|FILE_WRITE
open/create new [CSV|BIN] file for writing
IF file exists THEN contents cleared

[add/append+read to existing file] specify iMode of [FILE_CSV|FILE_BIN]|FILE_READ|FILE_WRITE
IF file !exist THEN file will be created via FILE_WRITE
***always*** FileSeek(iFh,0,SEEK_END) prior adding data
***always*** FileSeek(iFh,0,SEEK_END) prior adding data
***suggest*** caller issues FileSeek()

[read existing file] specify iMode of [FILE_CSV|FILE_BIN]|FILE_READ
only opens IF file exists
*/

Reason: