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
You cannot insert/remove data to/from the front or middle of an existing file, only to/from the end.
If your original file is small enough to fit entirely in memory, you can read the whole file into memory, manipulate it as needed, and then write it back out to a new file, overwriting the original.
Otherwise, you will need to read from the original file and write its data to a secondary file until you reach the position where you want to insert/remove data, then write/omit the desired data to/from the new file as needed, and then finish reading the original file and writing it to the new file. Then replace the original file with the new file.
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:
Advantages: Can handle a file twice the size of your memory, less copying.
Disadvantage: More iteration
Unless you're working on a file larger than 1/2 of your RAM, you will need to load the text file into RAM, detect it's length there, modify it, then write it back to disk. If the file is large then you would need to make a file stream driver, but unless you're trying to prevent SSD trashing then your best bet is the Read-Modify-Write method.