Efficient way to delete a line from a text file

前端 未结 8 1896
暖寄归人
暖寄归人 2020-11-29 11:09

I need to delete a certain line from a text file. What is the most efficient way of doing this? File can be potentially large(over million records).

UPDATE: below is

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 11:59

    Text files are sequential, so when deleting a line, you'll have to move all the following lines up. You can use file mapping (a win32 api that you can call through PInvoke) to make this operation a bit less painfull, but you surelly should considere using a non sequential structure for you file so that you can mark a line as deleted without realy removing it from the file... Especially if it should happen frenquently.

    If I've remember File Mapping Api should be added to .Net 4.

提交回复
热议问题