Write a program which reads a stream of numbers from a file, and writes only the positive numbers to a second file. The user should be prompted to enter the names of both the input file and output file in main(), and then main() will open both files. Another function named process() must then be called to read all the numbers from the input file and write the positive numbers to the output file. Note that you must pass the open stream variables for each file as arguments to the process() function, and that you need to (always) double check that the files opened successfully before using them.
This is what I have so far but its not working out!
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int process(ifstream &inf, ofstream &outf);
int main(void)
{
char in_name[100];
cout << "Enter name including path of input file: ";
cin >> in_name;
cout << "So, I will read numbers from " << in_name << endl;
ifstream inf;
inf.open(in_name);
if(!inf.is_open())
{
cout << "Error - Could not open " << in_name << endl;
cout << "Exiting\n";
exit(-1);
}
cout << "Ready to read from " << in_name << endl;
int numbers = process(inf,numbers);
inf.close();
cout << "Total: " << numbers << endl;
return 0;
}
int process(ifstream &inf, int numbers)
{
int numb, Pos, Total = 0,outf;
char numbin[100];
numbers = 0;
while(!(inf.eof()))
{
inf.getline(numbin, 8);
numb = atoi(numbin);
Total += numb;
cout << numbin << endl;
Pos = !(numb & 1);
if (Pos)
outf << numb;
}
return Total;
}