0

I have a file in which i have some details:

11 apple 13 
15 banana 14
16 grapes 19

Now i will search for 14 and i got to know that it's in 2nd line. So here i have two options. I can do

11 apple 15 
15 banana 50  //Modify that value 
16 grapes 19

Or

11 apple 15 
16 grapes 19

delete that line for in file.

I can do 2nd easily by creating a new file and copying the content of the original file except for that line.

But i found that unproductive. If i have 1 million such lines and deletion is frequent operation i can't do this every time.

Any idea of how to do 1st operation (replacing that particular value) and better way to do second one?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Yups
  • 1

1 Answers1

1
  1. Load (read) whole file into memory.
  2. Peform all replacements and deletions in memory using for example memove.
  3. Save (write) final memory buffer to a file.
4LegsDrivenCat
  • 1,247
  • 1
  • 15
  • 24