I need help with subtracting two text files

How would I subtract two text files from each other, and the output gets stored into another text file?

As you can see in my code, lets say averageRainfall.txt stores 1,2,3,4,5
while rainfallToDate.txt has 7,3,5,7,4. How would I write the code to have averageRainfall.txt subtract rainfallToDate.txt and storing it in rainfall.txt?

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>  // for cout
#include <fstream>   // for file I/O
#include <cstdlib>   // for exit()

using namespace std;

int main()
{
    ifstream fin;
    ofstream fout;
    ifstream fin_rainFall("rainfallToDate.txt");
    ifstream fin_average("averageRainfall.txt");

    if (fin.fail())
    {
        cout << "Input file failed to open.\n";
        exit(-1);
    }
    fout.open("rainfall.txt");
    if (fout.fail())
    {
        cout << "Output file failed to open.\n";
        exit(-1);
    }

    fout << "Rainfall for Cupertino: A Comparison\n" << endl;
    fout << "Month\tAverage\t 2015\tDeficit\n" << endl;

       for (int i = 1 ; i <= 12 ;i++) { // counts the month from 1-12

        fout<<i<<"\t";
        char num[256];

        fin_average.getline(num,256);
        fout<<num<<"\t";

        fin_rainFall.getline(num,256);
        fout<<num<<"\t";

        }


    fin.close();
    fout.close();

    return 0;

}
Read from input files, perform the subtraction, and write to the output file. E.g.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <fstream>

int main() {
    std::ifstream inA("averageRainfall.txt");
    std::ifstream inB("rainfallToDate.txt");
    std::ofstream out("rainfall.txt");

    int a;
    int b;
    while (inA >> a && inB >> b) {
        out << (a - b) << ",";

        // ignore delimiters
        inA.get();
        inB.get();
    }

    return 0;
}
So I have implemented what you wrote in my code, however, it doesn't print in the order as I want the numbers are all over the place.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
       for (int i = 1 ; i <= 12 ;i++) { // counts the month from 1-12
        char num[256];
        char num2[256];

        fout<< i << "\t";

        fin_average.getline(num,256);
        fout<<num << "\t";

        fin_rainFall.getline(num2,220);
        fout<<num2<< "\t";

        double a;
        double b;

        while (fin_average >> a && fin_rainFall >> b){
            fout <<"\t" << (a-b) << endl;

        }


That is how I changed the for loop, however what I get is this, http://imgur.com/WACrBo9
anyone want to help me out? I am studying for my final
Topic archived. No new replies allowed.