• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • 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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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