File Matching - C++ File Stream

C++ examples for File Stream:File Operation

Description

File Matching

Demo Code

#include <fstream>
#include <iostream>
#include <string>

int main(int argc, const char* argv[]) {
    // open oldmast.dat for input
    std::ifstream inOldMaster("oldmast.dat", std::ios::in);

    if (!inOldMaster) {
        std::cerr << "oldmast.dat could not be opened" << std::endl;
        return 0;
    }/*from   w w  w  .j a v a2 s . com*/
    // open transaction file for input
    std::ifstream inTransaction("trans.dat", std::ios::in);

    if (!inTransaction) {
        std::cerr << "trans.dat could not be opened" << std::endl;
        return 0;
    }
    // open newmast.dat for output (and creation)
    std::ofstream outNewMaster("newmast.dat", std::ios::out);

    if (!outNewMaster) {
        std::cerr << "newmast.dat could not be opened" << std::endl;
        return 0;
    }

    int transAccountNum = 0;
    int mastAccountNum = 0;
    double transDollarAmount;
    double mastDollarAmount;
    std::string fName;
    std::string lName;

    // process changes
    while (!inOldMaster.eof() && !inTransaction.eof()) {
        inOldMaster >> mastAccountNum >> fName >> lName >> mastDollarAmount;
        // ensure mastAccountNum !exceed transAccountNum
        if (transAccountNum < mastAccountNum)
            inTransaction >> transAccountNum >> transDollarAmount;

        // update total on account number match
        if (mastAccountNum == transAccountNum) {
            mastDollarAmount += transDollarAmount;
        } else if (((mastAccountNum > transAccountNum) && !inTransaction.eof()) || inOldMaster.eof()) {
            std::cout << "Unmatched transaction record for account number: "
                      << transAccountNum << std::endl;
        }

        if (!inOldMaster.eof())
            outNewMaster << mastAccountNum << " " << fName << " " << lName
                         << " " << mastDollarAmount << std::endl;
    }

    return 0;
}

Result


Related Tutorials