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

Posted on 2005-05-04
Last Modified: 2010-04-23
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..
Question by:AlexPonnath
    LVL 28

    Accepted Solution

    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.
    LVL 22

    Assisted Solution

    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

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now