|
-
Dec 6th, 2006, 11:45 AM
#1
Thread Starter
Frenzied Member
[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.
-
Dec 6th, 2006, 05:37 PM
#2
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.
-
Dec 6th, 2006, 06:27 PM
#3
Thread Starter
Frenzied Member
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?
-
Dec 6th, 2006, 07:10 PM
#4
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|