File is always empty...

 

Hello guys, I've tried to do some statistics/testing with EA and write it out into file but file always end up empty. 

Here is the opening and writing the info into it - it opens the file but does not write anything into it, what's the problem?

int OnInit()
{
   ZeroMemory(TouchCount);
   lastCandleTime = Time[0];
   CandleCounter = 1;
   ResetLastError();
   FileHandle = FileOpen("swingtest_" + TimeToString(TimeCurrent()) + ".txt", 
                                        FILE_WRITE|FILE_TXT|FILE_READ);
   if (FileHandle < 0) {
       Alert("Could not open file for write.");
       Alert(GetLastError());
       return(INIT_FAILED);
   }  

    FileWrite(FileHandle, "symbol: " + Symbol() + "\n"); 
    FileWrite(FileHandle, "timeframe: " + Timeframe() + "\n");
    FileWrite(FileHandle, "time: " + TimeToStr(TimeCurrent()) + "\n");
    FileWrite(FileHandle, "----------------------------------------------\n");
    FileFlush(FileHandle);
   return(INIT_SUCCEEDED);
}
 
TimeToString(TimeCurrent())

returns something like 2016.09.23

I think that the "." s in the filename confuses the system as it is expecting a file type to follow the "."

Try it with a different file name.

 
GumRai:

returns something like 2016.09.23

I think that the "." s in the filename confuses the system as it is expecting a file type to follow the "."

Try it with a different file name.

@GumRai: Yes, you are correct, but it is not the "." in the date that is the problem - it is the ":" in the time that is invalid for filenames. Having multiple "." in the filename is allowed, but the ":" is reserved for the device/disk separator.

@PeterBocan. The FileWrite() function is usually used for CSV output which includes outputting the the CR+LF ("\r\n") at the end automatically, so you don't have to add the "\n" at the end. Also, I don't see a FileClose() in your code. Is there one, like maybe in the OnDeinit()?

PS! You are using the flags "FILE_WRITE|FILE_TXT|FILE_READ" but unless you intend to read it back within the EA, you should just use "FILE_WRITE|FILE_TXT". However, if you intend to look at the contents from an outside source at the same time while the EA runs, use the following "FILE_WRITE|FILE_TXT|FILE_SHARE_READ".

 
    FileWrite(FileHandle, "----------------------------------------------\n");
    FileFlush(FileHandle);
   return(INIT_SUCCEEDED);
}
You don't close the file. You can't read from it until you do.
 

@GumRal @FMIC is right. 

@FMIC you got the cookie, you are right!

@WHRieder I do, in OnDeinit ;)

Reason: