If you are attached to a flat file, the simplest thing is repeated through each line, writing a new file and changing the one that matches.
Yes, that sucks.
I highly recommend switching to the βrightβ database. If you are concerned about the resources or the complexity of the server, you can explore SQLite or Berkeley DB. Both of them use a database, which is "just a file", eliminating the problem of installing and maintaining a database server, but nevertheless you can quickly and easily search, replace and delete individual records. If you still need a flat file for some other reason, you can easily write some import / export procedures.
Another interesting feature if you want to be creative is to look at your file system as a database. Give each user a directory. Each directory has a file for locations. In each file, update the resources. This means that to insert a line, you simply write to a new file. To update a file, you simply rewrite one file. Removing a user is just starting a directory. Undoubtedly, there is a bit more overhead to keep it all in mind.
Other ways to solve the problem may be to make your flat file write-only, since adding to the end of the file is a trivial operation. Then you create a second file that shows the dead line numbers that should be ignored when reading a flat file. In the same way, you can easily βXβ from existing lines (which again is much easier than trying to update lines in a file that might not be the same length) and add your new data to the end.
These two ideas are not really intended for practical solutions, as they show you that there is always more than one way to solve a problem.
source share