Hi to all, i need to  implement ofstream outfile code. Here is my code.How can i implement the ofstream outfile to ,so that the contents of the collection are re-written to the file before the application terminates.
#include "stdafx.h"

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
      int list[12];
        int option;

          int total, largest, smallest, occurrences, scale_factor;

          double average;

        //  cout << "Enter 12 positive integers for the list\n";
      ifstream infile("data.dat");


          for (int i = 0; i < 12; i++)

                  infile >> list[i];



       //   int option;

        //  int total, largest, smallest, occurrences, scale_factor;

        //  double average;



do

          {

                   cout << "\n\n\n";

                   cout << "\t0.      Display\n";

                   cout << "\t1.      Total\n";

                   cout << "\t2.      Average\n";

                   cout << "\t3.      Largest\n";

                   cout << "\t4.      Smallest\n";

                   cout << "\t5.      Occurrences of value\n";

                   cout << "\t6.      Scale Up\n";

                   cout << "\t7.     Re-write file\n";

                   cout << "\t99.    To Exit\n";

                   cout << "\t\tEnter option : ";

                   cin >> option;

                   cout << endl << endl;



                   switch (option)

                   {

                   case 0:

                             cout << "\t\t\t\tList is now\n";

                             for ( int i = 0; i < 12; i++)

                                      cout << "\t\t\t\t" << list[i] << endl;

                             break;

                   case 1:

                             total = 0;

                             for (int i =0; i < 12; i++)

                                      total += list[i];

                             cout << "\t\t\t\tTotal is " << total << endl;

                             break;

                   case 2:

                             total = 0;

                             for (int i =0; i < 12; i++)

                                      total += list[i];

                             average = (double)total/12;

                             cout << "\t\t\t\tAverage is " << average << endl;

                             break;



case 3:

                             largest = 0;

                             for (int i =0; i < 12; i++)

                                      if (largest < list[i])

                                                largest = list[i];

                             cout << "\t\t\t\tLargest is " << largest << endl;

                             break;

                   case 4:

                             smallest = list[0];

                             for (int i =1; i < 12; i++)

                                      if (smallest > list[i])

                                                smallest = list[i];

                             cout << "\t\t\t\tSmallest is " << smallest

 << endl;

                             break;#include "stdafx.h"

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
      int list[12];
        int option;

          int total, largest, smallest, occurrences, scale_factor;

          double average;

        //  cout << "Enter 12 positive integers for the list\n";
      ifstream infile("data.dat");


          for (int i = 0; i < 12; i++)

                  infile >> list[i];



       //   int option;

        //  int total, largest, smallest, occurrences, scale_factor;

        //  double average;



do

          {

                   cout << "\n\n\n";

                   cout << "\t0.      Display\n";

                   cout << "\t1.      Total\n";

                   cout << "\t2.      Average\n";

                   cout << "\t3.      Largest\n";

                   cout << "\t4.      Smallest\n";

                   cout << "\t5.      Occurrences of value\n";

                   cout << "\t6.      Scale Up\n";

                   cout << "\t7.     Re-write file\n";

                   cout << "\t99.    To Exit\n";

                   cout << "\t\tEnter option : ";

                   cin >> option;

                   cout << endl << endl;



                   switch (option)

                   {

                   case 0:

                             cout << "\t\t\t\tList is now\n";

                             for ( int i = 0; i < 12; i++)

                                      cout << "\t\t\t\t" << list[i] << endl;

                             break;

                   case 1:

                             total = 0;

                             for (int i =0; i < 12; i++)

                                      total += list[i];

                             cout << "\t\t\t\tTotal is " << total << endl;

                             break;

                   case 2:

                             total = 0;

                             for (int i =0; i < 12; i++)

                                      total += list[i];

                             average = (double)total/12;

                             cout << "\t\t\t\tAverage is " << average << endl;

                             break;



case 3:

                             largest = 0;

                             for (int i =0; i < 12; i++)

                                      if (largest < list[i])

                                                largest = list[i];

                             cout << "\t\t\t\tLargest is " << largest << endl;

                             break;

                   case 4:

                             smallest = list[0];

                             for (int i =1; i < 12; i++)

                                      if (smallest > list[i])

                                                smallest = list[i];

                             cout << "\t\t\t\tSmallest is " << smallest

 << endl;


                   case 5:

                             occurrences = 0;

                             int value;

                             cout << "Enter a value : ";

                             cin >> value;

                             for (int i = 0; i < 12; i++)

                                      if (list[i] == value)

                                                occurrences++;

                             cout << "\t\t\t\tNumber of occurrences is "

 << occurrences << endl;

                             break;

                   case 6:

                             cout << "Enter the scale factor :";

                             cin >> scale_factor;

                             for (int i = 0; i < 12; i++)

                                      list[i] *= scale_factor;

                             break;





                   case 99:

                             break;



                   default :

                             cout << "invalid option\n";

                   }

          } while (option != 99);




    return 0;
}

your program already reads the file -- just to the opposite of that loop starting at line 319.

using ofstream is like ifstream :
ofstream outfile=("out.txt");
outfile<<yourdata;
outfile.close();

this is how we write to a file

#include <iostream>
#include <fstream>

using namespace std;

int main () {
int What_to_write;
fstream myfile;
myfile.open ("filename.txt");
myfile << What_to_write;
myfile.close();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.