title: How to let user write into the file in c++ using put() function?
body: <p>I'm writing a c++ program to open a file with the user-defined file name. And then using the put() function to let the user write into the file until a newline character is encountered.
  But on running the program I'm not able to write into the file.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main()
  {
     fstream outfile;
     char name[20];
     cout&lt;&lt;&quot;Enter the name of file to be opened&quot;&lt;&lt;endl;
     cin.getline(name, 20);
     outfile.open(name, ios::out );
     if(!outfile){
         cout&lt;&lt;&quot;File cannot be opened&quot;&lt;&lt;endl;
         return 1;
     }
     char ch;
     cout&lt;&lt;&quot;File opened successfully&quot;&lt;&lt;endl;
     cout&lt;&lt;&quot;Write into the file&quot;&lt;&lt;endl;
     outfile.seekp(0);
     while(ch != ' '){
     outfile.put(ch);
     }
     return 0;
  }
  </code></pre>