0

I clarify the question: I need to delete specific line from a file (I know the contents of the line and its number) but I need to do it **without overwrite the entire file. **

Any suggestions or examples would be greatly appreciated!

efi
  • 11
  • 1
  • 2
    Why can't you overwrite the file? You kind of have to. – LarsTech Mar 10 '21 at 19:35
  • 2
    What exactly are you trying to accomplish? What if you capture the last modified date, make your change, and then reset the modified date to the original. Would that work? Please add details – Rufus L Mar 10 '21 at 19:36
  • 2
    You have to overwrite the file from the byte where it changes to the end (unless you are replacing some bytes, in which case you would only have to write that replacement). That's how the filesystem works. – Brannon Mar 10 '21 at 19:39
  • 1
    Depends on what/why you need original and updated but could you copy the file to a new location first, delete your row and save it. It wont overwrite the existing file. – Brad Mar 10 '21 at 19:41
  • That's part of a task I was given. I have a file with rows of data and I need to delete one of them without overwriting it (The line is not at the end of the file) @brannon can you explain in more detail how to do this? – efi Mar 10 '21 at 19:48
  • Are you sure the "overwrite" part is referencing the file itself? When you read the contents of the line, maybe you're supposed to skip the record in question and then save those results to the file. – LarsTech Mar 10 '21 at 20:01
  • How should a line be deleted from the file, whithout overwriting it? It wouldn't be the same file anymore. If you fear, that you need to read everything in memory, you could do it line for line using streams, like in the duplicate, I will now suggest. – Christian Gollhardt Mar 10 '21 at 20:18
  • Modifying a file overwrites it, unless you have a different definition of "overwrite". I know it's late now, but it would be helpful if you [edited](https://stackoverflow.com/posts/66571656/edit) your question to include more details and address some of the comments. It's not clear if you don't know how to read a file, don't know how to write a file, don't know how to change the "last modified" date of a file, or if the question is "how can I make changes to a file without overwriting it" then the answer depends on what you mean by overwirting, but generally the answer is "you can't". – Rufus L Mar 10 '21 at 22:03

2 Answers2

0

You will have to overwrite the file - it is the nature of saving the file.

You should:

  1. Read in your text file.
  2. Delete the line from the text read in
  3. Overwrite the file with the modified text variable.

The text will now be what you are looking for.

ServerS
  • 452
  • 3
  • 15
-1

Well, you could write a copy of your file minus the line you want to have deleted, then delete the original file and rename the copy to the original's file name. Technically speaking, there's won't be anything overwritten this way, although it only makes sense if the question they asked you was a riddle.

Heinz Kessler
  • 1,610
  • 11
  • 24