[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

How can i delete a specific row from a text file ?

i have a program which reads line by line from a txt file via streamreader. Once it reads the line
and the line meets a certain condition i would like to delet this line. One thing to mention the files
i am processing is very large its 40 Million + lines..
2 Solutions
You need to read file and if the line does not meet condition for deleting - write it to a different file. After file is written, delete the original file and then rename the new file to the original name.
Files are actually immutable blocks of data on the disk.
As mentioned, the only way to remove text or add text is to re-write the file.

However, an alternative could be to replace the text line with an equal number of "delete" characters such as spaces so that the file does not necessarily get re-written.  this would typically require opening the file as a random access data stream, then resetting your pointer to the byte where the line begins then put the correct number of replacement characters.
This would be more efficient on very large files, but certainly more programming.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now