fstream - How to write to middle of a file in C++? -


i think should quite simple, googling didn't far... need write existing file in c++, not @ end of file.

i know when want append text file, can pass flag ios:app when calling open on stream object. however, let's me write end of file, not middle.

i made short program illustrate issue:

#include <iostream> #include <fstream>  using namespace std;  int main () {    string path = "../test.csv";    fstream file;   file.open(path); // ios::in , ios::out default    const int rows = 100;   (int = 0; < rows; i++) {     file << << "\n";   }      string line;   while (getline(file, line)) {     cout << "line: " << line << endl; // here append more text rows   }     file.close();  } 

you cannot insert in middle of file. have copy old file new file , insert whatever want in middle during copying new file.

otherwise, if intend overwrite data/lines in existing file, possible using std::ostream::seekp() identify position within file.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -