Writing back changes to a file

Started by mcvpjd3, September 29, 2014, 07:02:38 AM

Previous topic - Next topic

mcvpjd3

Hi All, hopefully you can help with this.

I'm writing an application that has an input of a CSV. The app then read the relevant line(s) in the CSV for that user and based on that does whatever action is necessary. The last column in the CSV is currently a columns of 0s, I'd like to update that with a 1 to signify that the action has occurred (or maybe a different number based on the outcome of the action).

Now I could just change the data, and write the whole file back with the change, but it's a biggish file and lots of these users will be doing this over a WAN and possibly as the same time, so I'd rather not lock the file for a complete write back for each user. Is there an easier way of writing this change back to the file?

Thanks

JTaylor

Multiple users writing to the same file at the same time?   This will not end well.   I don't think you can avoid the locks and even if you did, if I have it open and you have it open and you save your changes and then I save my changes your changes will be lost.  At least I am having trouble thinking of a way to make that work from the same file with multiple users at the same time.

Why not use a database?

Jim

DAG_P6

I think Jim is right; this won't end well. This use case begs for a data base; it doesn't have to be complicated, but a one-table data base lets you leave the concurrency issues mostly up to the database engine.
David A. Gray
You are more important than any technology.

JTaylor

If for some reason you can't use a database you might consider writing the data for the user (sounds like a user will only be changing a certain section) to a separate file and  have a controlling script that is running all the time make the changes.   Using ArrayFileGetCSV() and ArrayFilePutCSV() might be useful.   The controlling script would check a subdirectory for data to change (use a file naming convention that is meaningful) and once it makes the change it could move the file to a different subdirectory.   Occasionally you could write out all the changes and then delete all the files from the second subdirectory.  This gives you a way to make sure you don't lose any data.   If it is a case where the same user is changing the same section multiple times during the day then you could check your subdirectories to see the current state of the data for that user in case it hasn't been written yet.  You will still need to account for the file locking when the controlling script periodically writes out the changes.   That wouldn't have to be that often though and you could just pop-up a message to the user to wait while that event is occurring.

Just a thought...

Jim