开发者

Remove Text From File and Shift

I am writing a program using VB6 and i can write and read to a file using the System.IO.StreamWr开发者_运维百科iter and System.IO.StreamReader. Now i am trying to delete an entry in the file and shift the remaing entry's up to prevent a blank line. Example:

a.txt has

1 2 3

i want to delete "2" and a.txt should now have

1 3

i don't want it to have

1 <--space 3

that is how i have it now i was wondering if anyone knows of a way of achieving this?


First of all, based on those class names I'm pretty sure you're using VB.NET, not VB6. You might get more responses if this really is about VB.NET, since more people know about it.

To answer your question, though: I doubt it's possible without rewriting the entire file from that point on. Think of files as an array of bytes, not a linked list of lines. As with an array, you can overwrite but you can't insert or delete as a constant-time operation. It might be possible to do a block read and a block write, but for small files the simplest thing to do will be to read the whole thing in as a list of line strings, delete a line from the list, and then write it back.


replace the text in the file with 1 3 <- space, if the effect needs only to be optical and trailing spaces are not a problem for your application

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜