1

I want to edit the first 100 characters of a file, I do this, but the new characters override the previous ones (like the photo) click here photo

my code :

fstream fileStreamIn("text.txt", ios::in | ios::out | ios::binary);

int theSize = 100;
string theMainBuffer(theSize, '\0');
fileStreamIn.read(&theMainBuffer.front(), theSize);
theMainBuffer.resize(fileStreamIn.gcount());
//cout << theMainBuffer << endl;
fileStreamIn.close();


fileStreamIn.open("text.txt", ios::in | ios::out | ios::binary);
fileStreamIn  << "blahblah ";
fileStreamIn.close();

I want "blahblah" to be added to the contents of the file and the previous contents of "helloworld" not to be deleted

output :

blahblahrld !
è !©ª}2•¼Ü²ù­XkLÉ·ð„!ð–ç„ñWïðʃ¡ åñ·§Dß}ˆ¹mÐÕŠw:—*ËtMÒJf-Öù“hñ<³:rÛä‡   ”‘Ôyv-4mXþeß§zè’¬ŒŽ<¤‘“‰l'g‚Šâ¡;¬Èa|ÔÁ3îú€;‰±Ï.ÖLáÑȽ[ïÿÿúU%ã2§Ls§n~çˆÏÔäÔ™ 4øÒ‘Ö°,y•»Ô'`` ¬ÜgÜò`÷Tº^E1ØàùÛ÷i§d¨Ù`I5»7á8Zéz0¥Ž’3Y7Êœ¦}eíÝΦIm?óbÙOâ-ŸäëŠgýhýR
Â3‘†y±è±/VФ?Ïù4?’ÑûIÆLQ~DãŠ?Ôêð#N ]³böPK     ZQamë  š  PK      5   -    

I want this output :

blahblah hello world !
è !©ª}2•¼Ü²ù­XkLÉ·ð„!ð–ç„ñWïðʃ¡ åñ·§Dß}ˆ¹mÐÕŠw:—*ËtMÒJf-Öù“hñ<³:rÛä‡   ”‘Ôyv-4mXþeß§zè’¬ŒŽ<¤‘“‰l'g‚Šâ¡;¬Èa|ÔÁ3îú€;‰±Ï.ÖLáÑȽ[ïÿÿúU%ã2§Ls§n~çˆÏÔäÔ™ 4øÒ‘Ö°,y•»Ô'`` ¬ÜgÜò`÷Tº^E1ØàùÛ÷i§d¨Ù`I5»7á8Zéz0¥Ž’3Y7Êœ¦}eíÝΦIm?óbÙOâ-ŸäëŠgýhýR
Â3‘†y±è±/VФ?Ïù4?’ÑûIÆLQ~DãŠ?Ôêð#N ]³böPK     ZQamë  š  PK      5   -    

What is the problem, how can I solve the problem? thanks

2
  • If you want to edit, you want to overwrite them, right? Or do you want to prepend the characters? In the latter case, that is not automatically possible, you would have to manually shift the file's content. Commented Aug 7, 2020 at 12:06
  • I want the contents of the file to remain, and I want to edit it Commented Aug 7, 2020 at 12:23

2 Answers 2

1

If you don't care to keep the first 100 bytes, simply create 100 lengths of string, change some values and write it to the stream would be enough. Reading a file is not needed.

std::fstream fs("text.txt", ios_base::out | ios_base::binary);

string buffer(100, ' ');
string update="Hello";
buffer.replace(0, update.size(), update);

fs.seekp(20);   // move to write position
fs.write(buffer.data(), buffer.size());

fs.close();
Sign up to request clarification or add additional context in comments.

5 Comments

thank you ,My problem is not solved, New characters are still placed on top of existing characters
@NewCoder, modify the size of buffer and writing can help you. Fill the buffer with empty characters,(100 chars) change the content of buffer(20 chars) and write to the stream without resizing the buffer.(100 chars) I only showed how to correctly modify bidirectional file stream.
Thank you, if you have time, give a more complete example
I made some changes on the answer.
Thank you dear friend, I updated my question, I think you did not understand my question
0

Use ios::trunc as the file open mode. For more info check out this.

2 Comments

I want the contents of the file to remain, and edited
If you want to append new and shift the already existing content then you have to read the whole file into string then write it back. or if file is too big then create a new file add the new content that you want to add and then read from old one and write in new one block by block then use rename (in cstdio or stdio in c++) on new file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.