in reply to Re^2: Practicing with files
in thread Practicing with files
I'm totally unclear on what you want to have happen. You want to force the user to type 'grape' three times before the program removes grape once? Seems to be an unlikely need.
You want to look at line one of the file, take user input, and if line one matches the user input remove it, then move on to the next line, and so on?
Is that what you're looking for? As I mentioned in a previous post, a single flat newline delimited file where each line can be of arbitrary length is not well suited to this task. The best I can suggest is to open a second file for output. At step one, write Apple to the second file. At step two, write Bananna to the second file. At step three, do not write Grape to the second file. At step four, close the output file, and rename it to take the place of the original input file.
In this way, you're essentially re-writing the original file by making a copy that has whatever modifications you need, and then replacing the original with the copy.
A newline-delimited file where lines may be of arbitrary length is not suited for in-place editing. You almost always have to write out a copy.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Practicing with files
by Jabox (Sexton) on Apr 01, 2014 at 20:20 UTC | |
by GrandFather (Saint) on Apr 02, 2014 at 01:33 UTC | |
by Discipulus (Canon) on Apr 02, 2014 at 08:01 UTC | |
by Jabox (Sexton) on Apr 02, 2014 at 13:09 UTC | |
by Jabox (Sexton) on Apr 02, 2014 at 14:00 UTC |