Results 1 to 4 of 4

Thread: [2.0] Remove a line

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    [2.0] Remove a line

    Alright, this is the last part of my program but I can't quite get it. Basically, I want to remove a line from a file. I have no clue where to start. Let's say my file is as follows

    Code:
    Jelly
    Pie
    Apple Pie
    Delicious..
    RoflCopter
    And let's say I want to remove line 3. I would want it to be

    Code:
    Jelly
    Pie
    Delicious..
    RoflCopter
    Thanks for any help. Btw, this is located in a txt file.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Remove a line

    It's quite simple, although it may not be immediately obvious. You can't actually delete a line from an existing text file. You basically have to read the file in, remove the line you don't want, then write out the new contents to the same location. There are various ways you could do it.

    1. Read in the entire file, then write it out line by line, excluding the line to be deleted.
    2. Read the file in line by line, discarding the line to be deleted, then write out the entire file.
    3. Read in the entire file, remove the line to be deleted from the data, then write out the entire file.
    4. Read the file in line by line and write to a temporary file line by line at the same, excluding the line to be deleted, then move the temporary output file to the original location, overwriting the existing file.

    They'll all do the job, so it's up to you which you prefer. The thing to note is that you can't read from a file and write to it at the same time, which is why the fourth option uses a temp file as intermediate output.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Remove a line

    JM, I'm not really good with reading files as I am with other stuff in C#. Could you perhaps show me some code examples to get me started?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Remove a line

    nevermind my friend is helping me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width