title: C++ - Modify a File Without Creating a New File
body: <p>I'm trying to open a file, and modify some contents inside. My first code looks like this,</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main(int argc, char** argv) {
      int status = 0;
  
      //error if no filename
      if(argc == 1) {
          cerr &lt;&lt; "No file specified. Please specify a filename" &lt;&lt; endl;
          status = 1;
      }
  
      //open a file and modify
      for (int i = 1; i &lt; argc; ++i) {
          string line = "";
          string filename = argv[i];
          ifstream infile;
  
          infile.open(filename);
          if(infile.fail()) {
              status = 1;
              cerr &lt;&lt; filename &lt;&lt; "： No Such File Exist" &lt;&lt; endl;
          }
          else{
              while(getline(infile, line)) {
                  auto index1 = line.find('(');
                  auto index2 = line.find(')');
                  cout &lt;&lt; index1 &lt;&lt; "   " &lt;&lt; index2 &lt;&lt; endl;
                  auto itor = line.begin();
                  if(index1 != string::npos)  line[index1] = '[';
                  if(index2 != string::npos)  line[index2] = ']';
              }
              infile.close();
          }
      }
  
  
      return status;
  }
  </code></pre>
  
  <p>I know it's wrong to directly modify <code>line</code> because it won't change the content in the file. Is there a way that I can modify the file directly?(Without creating a new file, and output <code>line</code> to that)</p>