Greeting everyone,

Before you read, I've posted all of the code in the program in case someone wanted to see exactly what I was doing, but the main area I'm having issues with is writing to outFile in the writeOutputFile function.

I'm attempting to write a simple find and replace program. There are 2 input files, small.txt and array.txt, and the number of output files is based upon the number of rows in array.txt.

small.txt is the template file that I would like to search through and then replace with the (partially) user defined string.

array.txt is really a CSV file with 2 columns: TariffID and TariffServiceCode
Ex.)
12345,ABC
23456,BCD
34567,CDE

The program searches the template file (small.txt) line by line and replaces the string "<TariffID>YYYYY</TariffID>" or "<TariffServiceCode>ZZZZZ</TariffServiceCode>" with a similar string, except YYYYY and ZZZZZ are different. YYYYY and ZZZZZ correspond to the list of values in array.txt. After each line, whether it was replaced or not, it is then written to an output file. Each output file has the YYYYY portion of the CSV file prepended to the filename supplied by the user.

So far, all of the find and replace functionality works (but yes, does look messy). The issue I'm having is when I run the program, the first output file has the correct data in it (including the replaced text) along with the correct filename, but the second file and after have the correct filenames but are blank inside.

Code:
#include<iostream>
#include<fstream>
#include<string>
//#include<sstream>

using namespace std;

//#define

//Globals
ifstream inFile;
ifstream arrayFile;
ofstream outFile;

string tariffID;
string longTariffID;
string tariffServiceCode;
string longTariffServiceCode;

//prototypes
void writeOutputFile(string&, string, string, string, string, int, int);

int main()
{
    int lengthLongTariffID = 0;
    int lengthLongTariffServiceCode = 0;

    string arrayFileString;
    string arrayFileTariffID;
    string arrayFileLongTariffID;
    string arrayFileTariffServiceCode;
    string arrayFileLongTariffServiceCode;

    string filename;
    string filenameTariffID;
    string lastFilenameTariffID = "";

    size_t found;
    int foundINT;

    size_t lengthOldTariffID;
    int INTlengthOldTariffID;

    size_t lengthOldTariffServiceCode;
    int INTlengthOldTariffServiceCode;

    int x;
    int y;

    inFile.open("small.txt");
    arrayFile.open("array.txt");

    cout << "\n\nWhat do you want the filename of the output to be?\nPlease use underscores instead of spaces and include the file extension (.txt):  ";
    cin >> filename;

    cout << "What is the Tariff ID you want to change?:  ";
    cin >> tariffID;
    lengthOldTariffID = tariffID.length();
    INTlengthOldTariffID = int(lengthOldTariffID);

    cout << "What is the Tariff Service Code you want to change? (Capitalization counts):  ";
    cin >> tariffServiceCode;
    lengthOldTariffServiceCode = tariffServiceCode.length();
    INTlengthOldTariffServiceCode = int(lengthOldTariffServiceCode);

    longTariffID = "<TariffID>" + tariffID + "</TariffID>";
    longTariffServiceCode = "<TariffServiceCode>" + tariffServiceCode + "</TariffServiceCode>";

    //Length of "<TariffID>" = 10 and Length of "</TariffID>" = 11
    lengthLongTariffID = 10 + INTlengthOldTariffID + 11;
    //Length of "<TariffServiceCode>" = 19 and Length of "</TariffServiceCode>" = 20
    lengthLongTariffServiceCode = 19 + INTlengthOldTariffServiceCode + 20;

    while( getline(arrayFile, arrayFileString) )
    {
        found = arrayFileString.find(",");
        foundINT = int(found);

        //Builds arrayFileLongTariffID from arrayFile to compare to longTariffID
        arrayFileLongTariffID = "<TariffID>";
        for (x = 0; x < foundINT; x++)
        {
            arrayFileLongTariffID += arrayFileString[x];
            filenameTariffID += arrayFileString[x];
        }
        arrayFileLongTariffID += "</TariffID>";

        //Builds arrayFileLongTariffServiceCode from arrayFile to compare to longTariffServiceCode
        y = foundINT + 1;
        arrayFileLongTariffServiceCode = "<TariffServiceCode>";
        while( (arrayFileString[y] != '\n') && (arrayFileString[y] != '\r') && (arrayFileString[y] != '\0') )
        {
            arrayFileLongTariffServiceCode += arrayFileString[y];
            y++;
        }
        arrayFileLongTariffServiceCode += "</TariffServiceCode>";

        writeOutputFile(lastFilenameTariffID, filename, filenameTariffID, arrayFileLongTariffID, arrayFileLongTariffServiceCode, lengthLongTariffID, lengthLongTariffServiceCode);

        filenameTariffID = "";
    }

    inFile.close();
    arrayFile.close();

    return 0;
}

void writeOutputFile(string &lastFilenameTariffID, string filename, string filenameTariffID, string arrayFileLongTariffID, string arrayFileLongTariffServiceCode, int lengthLongTariffID, int lengthLongTariffServiceCode)
{
    string outputFilename;
    string inFileString;
    size_t found2;
    size_t found3;
    int INTfound2;
    int INTfound3;

    //int position;

    if( (lastFilenameTariffID != filenameTariffID) && (filenameTariffID != "") ) //Make sure the TariffID is not duplicated, or empty because of eof
    {
        outputFilename = filenameTariffID + "_" + filename;

        outFile.open( outputFilename.c_str(), ios::trunc );

        /*
        if(outFile.is_open())
        {
            cout << "\n" << outputFilename << "\n";
        }

        position = outFile.tellp();

        cout << "\n" << position << "\n";
        */

        while( getline(inFile, inFileString) )
        {
            found2 = inFileString.find(longTariffID);
            found3 = inFileString.find(longTariffServiceCode);

            if( found2 != string::npos )  //If longTariffID is in this line from inFile
            {
                INTfound2 = int(found2);

                inFileString.erase(INTfound2, lengthLongTariffID);

                inFileString.insert(INTfound2, arrayFileLongTariffID);

                outFile << "\n\nTESTING\n\n";
                outFile << inFileString << '\n';
            }
            else if( found3 != string::npos )  //If longTariffServiceCode is in this line from inFile
            {
                INTfound3 = int(found3);

                inFileString.erase(INTfound3, lengthLongTariffServiceCode);

                inFileString.insert(INTfound3, arrayFileLongTariffServiceCode);

                outFile << "\n\nTESTING\n\n";
                outFile << inFileString << '\n';
            }
            else
            {
                outFile << "\n\nTESTING\n\n";
                outFile << inFileString << '\n';
            }
        }//END while( getline(inFile, inFileString) )
        //outFile.flush();
        outFile.close();
        outFile.clear();
    }//END if( (lastFilenameTariffID != filenameTariffID) && (filenameTariffID != "") )

    lastFilenameTariffID = filenameTariffID;
    inFile.seekg(0, ios::beg);
}
Since I was unable to attach any files to the post, small.txt is below. Keep in mind, this is just an example for testing, not actual code.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<CISDocument>
    <TariffEntity>
        <TariffService>
            <TariffID>33207</TariffID>
            <TariffServiceCode>TL</TariffServiceCode>
            <Service>Load</Service>
            <TariffCharge>
                <TariffID>33207</TariffID>
                <TariffServiceCode>TL</TariffServiceCode>
                <Charge>*ALL</Charge>
            </TariffCharge>
        </TariffService>
    </TariffEntity>
</CISDocument>
Some of the posts/sites I've found suggested using ofstream.clear() or .flush() to reset the flags that may have been thrown in a previous use of the variable, but this hasn't helped.

Any guidance or suggestions would be appreciated. Thanks in advance.

~D