Using FileOpen to protect a .csv

 

My EA creates, writes to, and reads from .csv files while it's running.  One issue I noticed was that if I open the .csv in Excel to read it, my EA will not be able to open it and write to it. Would it make sense to use FileOpen during initialization, and keep the .csv files open (and then close in deinit), thus preventing me from opening them in excel and interfering with the EA?  Or would I be creating problems by leaving these files open in the EA?  

 
Ian Tavener:

My EA creates, writes to, and reads from .csv files while it's running.  One issue I noticed was that if I open the .csv in Excel to read it, my EA will not be able to open it and write to it. Would it make sense to use FileOpen during initialization, and keep the .csv files open (and then close in deinit), thus preventing me from opening them in excel and interfering with the EA?  Or would I be creating problems by leaving these files open in the EA?  

Just make an excel folder and copy the file so you can still open the current version and not interfere with the EA working file.

 
Ian Tavener:

Would it make sense to use FileOpen during initialization, and keep the .csv files open (and then close in deinit), thus preventing me from opening them in excel and interfering with the EA? 

I'd probably go this route. The EA will then always have access to the file, and you don't have to remember not to look inside.

Or would I be creating problems by leaving these files open in the EA?  

The immediate problem that comes to mind is that you wouldn't be able to "peek" inside the file using Excel (or text editor), which is similar, but opposite to the problem you have now.

To remedy this situation, you could create a snapshot. For example, when the user presses the 'S' keystroke: close the file, copy to a new name, re-open the original file, seek to the end of the file. Now you can look inside the copy while your EA continues happily.

 
Ian Tavener:

My EA creates, writes to, and reads from .csv files while it's running.  One issue I noticed was that if I open the .csv in Excel to read it, my EA will not be able to open it and write to it. Would it make sense to use FileOpen during initialization, and keep the .csv files open (and then close in deinit), thus preventing me from opening them in excel and interfering with the EA?  Or would I be creating problems by leaving these files open in the EA?  

You may open a file with FileOpen() in shared mode, by specifying the flags FILE_SHARE_READ  or FILE_SHARE_WRITE. Hopefully Excel will be able to open the file, along with the EA 

regards

 
Anthony Garot:

I'd probably go this route. The EA will then always have access to the file, and you don't have to remember not to look inside.

The immediate problem that comes to mind is that you wouldn't be able to "peek" inside the file using Excel (or text editor), which is similar, but opposite to the problem you have now.

To remedy this situation, you could create a snapshot. For example, when the user presses the 'S' keystroke: close the file, copy to a new name, re-open the original file, seek to the end of the file. Now you can look inside the copy while your EA continues happily.

That snapshot idea is fantastic!!  I will also experiment with the FILE_SHARE flags. Thanks for the help, all.