title: Writing the text from a struct into a file via fstream
body: <p>I want to write the contents of the 6 films of the movies_iit struct into my file fout("data.dat").</p>
  
  <pre><code>// array of structures
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;istream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;cctype&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  #define NUM_MOVIES 6
  //###################################################################################################    ################
  //structure
  //###################################################################################################    ################
  struct movies_iit{
      string title;
      int year;
  } films [NUM_MOVIES];
  //global variables
  char title [20], y, n;
  int year;
  string search;
  //function 1
  void sort_on_title(movies_iit films[], int n)
  {
  //Local struct variable used to swap records
  movies_iit temp;    
  
  for(int i=0; i&lt;n-1; i++)
      {
  for(int i=0; i&lt;n-1; i++)
          {
  /*If s[i].title is later in alphabet than
  s[i+1].title, swap the two records*/
          if(films[i].title&gt;films[i+1].title)
              {
                  temp = films[i];
                  films[i] = films[i+1];
                  films[i+1] = temp;
              }  
          }
      }
  }
  //end function 1
  //function query1 prototype
  void query1 (movies_iit movie);
  //function query2 prototype
  void query2 (movies_iit movie);
  //function 2 prototype
  void printmovie (movies_iit movie);
  //###################################################################################################    ################
  //beginning of main
  //###################################################################################################    ################
  int main ()
  {
  //login
  //username: user
  //password: word
  string mystr;
  int n;
  char response;
  string pass;
  string name;
  //output object
  ofstream fout ("data.dat");
  cout &lt;&lt; "enter your username "&lt;&lt;endl;
  cin &gt;&gt; name;
  cout &lt;&lt; "enter your password "&lt;&lt;endl;
  cin &gt;&gt; pass;
  
  cout &lt;&lt; "\n" &lt;&lt; endl;
  
  if (name == "user" &amp;&amp; pass == "word")
      cout &lt;&lt; "Welcome, user." &lt;&lt; endl;
  else
  {cout &lt;&lt; "###" &lt;&lt;  "unrecognized username/password combination" &lt;&lt; "\t" &lt;&lt; "please try again"     &lt;&lt; "###" &lt;&lt; endl;
      system("PAUSE");
      return 0;
  }
  
  cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
  
  cout &lt;&lt; "\n" &lt;&lt; endl;
  
  for (n=0; n&lt;NUM_MOVIES; n++)
  {
      cout &lt;&lt; "Enter title: ";
      getline (cin,films[n].title);
      cout &lt;&lt; "Enter year: ";
      getline (cin,mystr);
      stringstream(mystr) &gt;&gt; films[n].year;
  }
  //sort records, function 1 call
  sort_on_title(films, NUM_MOVIES);
  cout &lt;&lt; "\nYou have entered these movies:\n";
  for (n=0; n&lt;NUM_MOVIES; n++) 
      printmovie (films[n]);  //function 2 call
  //Query 1
  cout &lt;&lt; "Perform an alphabetical search? (y/n)" &lt;&lt; endl;
  cin &gt;&gt; response;
  
  if (response == 'y')
      {cout &lt;&lt; "Please enter title" &lt;&lt; endl;
      cin &gt;&gt; title;
      for (n=0; n&lt;NUM_MOVIES; n++)
          {query1 (films[n]);
              response == n;
          }
      }
  else if (response == 'n')
      cout &lt;&lt; "\n" &lt;&lt; endl;
  else
      cout &lt;&lt; "invalid entry" &lt;&lt; endl;
  //Query 2
  cout &lt;&lt; "Perform a chronological search? (y/n)" &lt;&lt; endl;
  cin &gt;&gt; response;
  //greater than
  if (response == 'y')
  {   cout &lt;&lt; "greater than what year?" &lt;&lt; endl;
      cin &gt;&gt; year;
      for (n=0; n&lt;NUM_MOVIES; n++)
          {   query2 (films[n]);
          }
  }
  else if (response == 'n')
      cout &lt;&lt; "Thank you, goodbye." &lt;&lt; endl;
  else
      cout &lt;&lt; "invalid entry" &lt;&lt; endl;
  system("pause");
  return 0;
  }
  //###################################################################################################    ################
  //end of main
  //###################################################################################################    ################
  //function 2 definition
  void printmovie (movies_iit movie)
  {
    cout &lt;&lt; movie.title;
    cout &lt;&lt; " (" &lt;&lt; movie.year &lt;&lt; ")\n";
  }
  //function query1 defintion
  void query1 (movies_iit movie)
  {
    if (movie.title == title)
        {cout &lt;&lt; " &gt;&gt; " &lt;&lt; movie.title;
    cout &lt;&lt; " (" &lt;&lt; movie.year &lt;&lt; ")\n";}
    else
        {cout &lt;&lt; movie.title;
    cout &lt;&lt; " (" &lt;&lt; movie.year &lt;&lt; ")\n";}
  }
  //function query2 definition
  void query2 (movies_iit movie)
  {
    if (movie.year &gt;= year)
        {cout &lt;&lt; movie.title;
          cout &lt;&lt; " (" &lt;&lt; movie.year &lt;&lt; ")\n";
        }
  }
  </code></pre>
  
  <p>This code is currently designed to recieve 6 movie names and release dates (respectively) and allow a user to query through them.  I would now like to write the contents of each of the 6 elements as a seperate line in my file.  How would I go about doing that?</p>