How specific lines in a text file can be updated many times ?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
0 votes
Share a link to this question
Accepted Answer
1 vote
21 Comments
- create a new file each time, preserving the previous ones, leaving it for the user to clean them up as desired
- create a new file, rename the old file to ".bak" (or similar), rename the new file to the old name; in this way if the new file creation fails, there is always the .bak to go back to
- create a new file, rename it to the old name once it is fully created; keeping a backup like the above would be better, but at the very least, do not put the new file into the place of the old file until the new file has been completely created and verified to have worked
- in cases where preserving file dates and permissions (and alternative resource forks) is important and those cannot be easily copied from one file to another, then create a new file, write the data to it, verify it is all correct, then take the contents and use them to overwrite the old file in-place (because overwriting preserves dates and access permissions and resource forks.) In this set-up, there is always a file with valid data contents in existence in case something goes wrong while overwriting the file. Note: you cannot reliably do this in MATLAB, because MATLAB does not allow you to truncate existing files if they should shrink due to the new content being smaller
- in the more general case, use a "transactional database" that is able to roll-back failed or incomplete transactions; or if your file-system supports transactions (for example the native Mac OS-X file system does), find a way to make the update take only a single transaction for file-system purposes so that you can recover if something goes wrong.
More Answers (0)
Categories
Find more on Historical Contests in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)