How to insert char in middle of text file?

前端 未结 3 1215
醉梦人生
醉梦人生 2021-01-28 17:24

Firstly, I want to insert characters into a text file. I want to print the characters (DAD1) into the middle of the text file (HERE with DA D1) and the

3条回答
  •  悲&欢浪女
    2021-01-28 18:11

    The other answers are suggesting loading the entire file into memory, so I thought I would leave a comment:

    If memory is an issue, try the following:

    1. Iterate through file to determine its size (do not load into memory).
    2. Set file pointer to size/2 and read the latter half of the file into memory.
    3. Insert byte(s) at index of size/2.
    4. Append latter half of memory after inserted bytes.

    Advantages: Can handle a file twice the size of your memory, less copying.

    Disadvantage: More iteration

提交回复
热议问题