I got the desired results from my code but I do not know how to save it to a new
ID: 3555578 • 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
//Following program saves to "example.txt"
#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;
ofile.open ("example.txt");
while (file.good())
{
{
while (file.peek()==' ')
file.get();
}
getline(file,c);
ofile << (c) << endl;
}
system("pause");
ofile.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.