Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I got the desired results from my code but I do not know how to save it to a new

ID: 3555579 • Letter: I

Question

I got the desired results from my code but I do not know how to save it to a new file....

The question was:
Write a program that skips leading whitespace characters in each line of the provided data file CPPHumor.txt. Save the results in a new file.

If someone could help me find out how to save into a new file that would be awesome!

  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  
  #include <iostream>  #include <fstream>  #include <string>  #include <cstdlib>  using namespace std;    int main()  {          ifstream file;          ofstream ofile;          string c;          int i=1;            file.open("CPPhumor.txt");          if (file.fail())                  {                  cout<< " The file was not opened"<<endl;                  system("pause");                  exit(1);                  }          cout<<"file was opened"<<endl;                    while (file.good())                  {                          {                                  while (file.peek()==' ')                                  file.get();                          }                  getline(file,c);          cout << (c) << endl;                  }          system("pause");          return  0;  }  

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
        ifstream file;
        ofstream ofile;
        string c;
        int i=1;

        file.open("CPPhumor.txt");
        ofile.open("newfile.txt");
        if (file.fail())
                {
                cout<< " The file was not opened"<<endl;
                system("pause");
                exit(1);
                }
        cout<<"file was opened"<<endl;

        while (file.good())
                {
                        {
                                while (file.peek()==' ')
                                file.get();
                        }
                getline(file,c);
                ofile << c;

        cout << (c) << endl;
                }
        system("pause");
        return 0;
}

---------------------------------------------------------------

the changes are shown in bold..