Suppose I have a text file with this content: abcdefghk
I want to write a text at position index of 3 with a new text: xyz
In such a way that I will have a new text file: abcxyzghk
How can I achieve this in native C++?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just use
fseekto position and rewrite. If you need to insert, you should use another approach. First, open for appending (“a+t”), set position, and write.Note this is C++ code, and I put
FILE* funder if scope to avoid accidental usefafterfclose. Take care about possible I/O exceptions (fseekoutside the EOF).