Saving to a new file

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<< "\nThe 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;
}
Writing to a file is like writing to std::cout.
The file I'm bringing in as a text is an ifstream and the new file(altered original) is an ofstream correct?
yes. Also why are you doing
1
2
3
4
5
6
7
	while (file.good())
		{
			{
				while (file.peek()==' ')
				file.get();
			}
		getline(file,c);


I would do
1
2
3
4
while(std::getline(file, c))
{
    //do something with read information
}
I am kind of weary of using "std::", its foreign territory to me.
Topic archived. No new replies allowed.