Interrogating the 'error' value from an FileOpen statement - page 2

 
RaptorUK: I guess it depends of what you are doing with your files,  I'm currently coding some stuff to save arrays to files but the file will only be updated when there is new information to be saved not on every tick, so for me the overhead of opening and closing the files each time is not an issue,  if you are writing to the file(s) on each tick then it may be a different matter.

I'm experimenting with using File-Data as Variables. In other-words, I'm playing around with codes which have no Global_Scope nor Global_Variable_Set Variables defined. Yet can have as many Pseudo Global_Scope_Variables which could recover themselves like a Global_Variable_Set. I just started this project yesterday. Another reason for doing this is to get more familiar with Files, especially Binary-Files. The obvious trade-off here is Speed vs Resources [Memory].

The Speed will make a difference within the Strategy-Tester [this I already know] because it's slow-enough as-is. However, the question I'm trying to answer is "If this'll make a noticeable difference for a Live running Expert". My gut tells me the file-speed is good enough but there's only one way to find out.  

 
RaptorUK:
The point is that if a file is closed as soon as it is no longer needed to be open it has less risk of being open at the time of a crash . . . .  if I am working with 5 files and keep them open then they are open all the time while the EA runs, any crash during that long time period will result in 5 files being left open.

What happen if MT4 crash during its read/write operation, File Close does not help either :).


Don't worry about that, when MT4 crash or terminate (like ubzen did) the handle is gone, file is no longer in use.

See MSDN CreateFile which can be used for creating and opening a file, this will return unique identifier which we called : File Handle. Another way to open a file is using OpenFile function which also return File Handle.

MSDN for File Handles

"When a file is opened by a process using the CreateFile function, file handle is associated with it until either the process terminates or the handle is closed using the CloseHandle function. The file handle is used to identify the file in many function calls.

 
phi.nuts: Don't worry about that, when MT4 crash or terminate (like ubzen did) the handle is gone, file is no longer in use.

If MT4 crashes, sure the file is closed. But any buffered output will not be written and has been lost.

What happens if the EA thread crashes (e.g. divide by zero) What happens then?

 
phi.nuts:


"When a file is opened by a process using the CreateFile function, file handle is associated with it until either the process terminates or the handle is closed using the CloseHandle function. The file handle is used to identify the file in many function calls.

OK,  so you just need to make sure you sue FileFlush()  as  ubzen mentioned.
 
WHRoeder:

If MT4 crashes, sure the file is closed. But any buffered output will not be written and has been lost.

What happens if the EA thread crashes (e.g. divide by zero) What happens then?

The file is left open and inaccessible until the process is terminated . . .  this isn't a very satisfactory state of affairs  IMO
 
WHRoeder:

If MT4 crashes, sure the file is closed. But any buffered output will not be written and has been lost.

What happens if the EA thread crashes (e.g. divide by zero) What happens then?


... or CI or Script :

If MT4 associate file handle with an mql programs, then the handle is gone. If MT does not associate file handle with an mql programs, then we will have error too many opened files, sooner or later. In Win API its : System Error Code (0 -499) .  

I said if, coz I don't know for sure :)

Reason: