Get integers from txt file and output into another?

I'm writing a program that gets all the even integers in a txt file and outputs it to another txt file. I'm having trouble figuring out what to do.
Here's the code, i got the numbers, no how do I output it to a new file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string filename = "hello.txt";
int x;
ifstream input;

input.open(filename.c_str());
if (!input)
{
cout << "ERROR : File" << filename << " cannot be opened" << endl;
input.close();
}
while (input >> x )
{
cout<< x << " ";
}
Last edited on
Show some code and we'll be able to help.
Where are you having the trouble?
The code is up.
Where are you having the trouble?

Quoting. What's wrong? You know how to read from a file, you should be able to write to another file.
You aren't having bugs or exceptions.
You probably just don't want to finish the code by yourself?

EDIT: Last Minute Tip: ofstream.
Last edited on
You're missing a fair bit of logic. You probably want to read the numbers into an array, then loop through the array picking out all of the even ones.

Then you can look at outputting them using an output stream.
I haven't learned how to use array. If I want to output to a new file, can't I just us ofstream.

Don't judge me. I'm new to c++ and programming.
Topic archived. No new replies allowed.