c++ - Reading and writing in the same text file using a for loop -
is possible both read , write in text file using 1 for loop? want print out symbol .
@ 0 pos , after every 8 chars in text. here's code, doesn't work:
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream file("mnmn.txt", ios::out | ios::in ); string content; int i; while(file >> content) { (size_t = 0; < content.size(); i+=8) { file<<"."; break; } } return 0; }
ddta in text file is:
abcdefghijklmnop
i want become:
.abcdefgh.ijklmnop.
you cannot - data in file not "shift" when write character, gets written over.
a common approach solving follows:
- open temporary file writing. write dot it
- open source file reading. read 8 characters @ time
- every time read 8 characters source file, write them temp file; append dot
- once reading complete, close both files; move temporary file in place of original.
Comments
Post a Comment