title: c++ writing data to file without duplicates
body: <p>I have created some methods which will write song data to a text file.
  If the file doesn't exist it will create a new one.</p>
  
  <p>The problem I am having is that every time I run the application again it will append to the file and duplicate all the data.</p>
  
  <p><strong>This Method is called first</strong></p>
  
  <pre><code>void SongLibrary::SaveAllToFile(string fileName)
  {
      for (Song song : m_songs)
      {
          WriteToFile(song.ToFileString(),fileName);
      }
  }
  </code></pre>
  
  <p><strong>This method is called once for each song object</strong></p>
  
  <pre><code>void SongLibrary::WriteToFile(string data, string fileName)
  {
      if (FileExists(fileName))
      {
          ofstream outfile;
          outfile.open(fileName, ios::app);
          outfile &lt;&lt; data;
  
          outfile.close();
      }
      else
      {
          CreateFile(fileName);
          WriteToFile(data, fileName);
      }
  }
  </code></pre>
  
  <p><strong>This method creates a file</strong></p>
  
  <pre><code>void SongLibrary::CreateFile(string fileName)
  {
      ofstream outfile(fileName);
      outfile.close();
  }
  </code></pre>
  
  <p><strong>This method checks if a file exists</strong></p>
  
  <pre><code>inline bool SongLibrary::FileExists(const string&amp; fileName) 
  {
      struct stat buffer;
      return (stat(fileName.c_str(), &amp;buffer) == 0);
  }
  </code></pre>