Forum Moderators: open
I think this is the way a SQL server connection works
You can't compare a real database process with a text file.
A database has sophisticated row handling in place to update the one row you need updated. A text file, by nature, has to be read top to bottom to find the line to be updated.
You have some flexibility with text files, like a serach perhaps, but it still needs to be read top to bottom.
A database also has row and table locking to prevent the issues you describe.
Another method is to 'lock' the open file so that the second task cannot open the file (ruining the first file process). Some OS allow file locking easily, others are more difficult. One simple method is to create another file and store one '0' in it. Every program that updates the file (or writes to it in any way) should update that 0 to a 1 (which takes a fraction of a second). Then when finished, reset the 1 back to a 0. Only allow a program to open a file when the content of the second file is a zero - I would choose to repeatedly attempt to open the file every few seconds for a specified time limit. After that it's up to you if you want to stop the program with an error or go ahead and write to the file (bearing in mind another program may be still also writing or another program may have crashed, losing the reset of the 0/1 flag).
Using an OS lock can cause problems in that if you do not unlock the file, it may cause lack of access to that file for all other programs until you manually unlock it again (this may happen on a server crash/program crash etc...).
Thanks for all your suggestions.