tags:

views:

30

answers:

1

hello,

i have a very big text file, am using fgets() to get the contents of the file line by line. but i need to delete some text from the file if certain condition is met.

ex:

hello world am string number 1 hello world am string number 2

hello world am string A hello world am string B

lets say a condition is met in string 1, then i want to delete string 1 and 2, but do nothing to string A and B

+1  A: 
  1. Open this very big text file for reading. Call this File 1.
  2. Open a new file for writing. Call this File 2.
  3. Read a line from File 1.
  4. Check if the line satisfies your condition(s).
  5. If you want to keep the line, write it to File 2.
  6. If you want to throw away the line, do nothing.
  7. Read another line from File 1. Wash, rinse, repeat.
  8. When you're done, delete File 1, and rename File 2 to File 1. (Make sure you test multiple times before you do this -- it's not reversible!)
kijin
In other words: You can't delete a line from the middle of a file without rewriting the whole file.
Marc B
Yes, as @Marc B said, if you delete 10 bytes from the middle of a file, everything after that point needs to be shifted 10 bytes to the left. Might as well just create a new file.
kijin