title: How to open a file or create it if it doesn't exist and fill it with values from the console
body: <p>My goal is to write a program that will check if a file is exist and if it contains the values. If it true, the program should read these values from the file into an array if not create a file if it didn't exist and write values to a file from the console. I write next code:</p>
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main(void)
  {
      char filename[] = &quot;alphabet.txt&quot;;
      string alphabet;
      char ch;
      bool f;
      fstream fileWithAlphabet;
  
      fileWithAlphabet.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
      (fileWithAlphabet.peek() == std::ifstream::traits_type::eof()) ? f = true : f = false;
  
      if (f)
      {
          cout &lt;&lt; &quot;File empty, fill file&quot; &lt;&lt; endl;
          cout &lt;&lt; &quot;Fill the file with alphabet characters&quot; &lt;&lt; endl;
          cout &lt;&lt; &quot;please enter a single character, using q to stop: &quot; &lt;&lt; endl;
              for (;;) {
                  cin &gt;&gt; ch;
                  if (ch == 'q')
                      break;
                  fileWithAlphabet &lt;&lt; ch &lt;&lt; &quot;\n&quot;;
              }
          fileWithAlphabet.close();
  
      }
      else
      {    // use existing file
          cout &lt;&lt; &quot;success &quot; &lt;&lt; filename &lt;&lt; &quot; open. \n&quot;;
          cout &lt;&lt; &quot;read from a file&quot;;
  
          **** read into array ****        
  
          fileWithAlphabet.close();
  
      }
      return 0;
  }
  </code></pre>
  <p>Please say, why after executing the code and output from the console, the file remains empty?</p>