Using 1 ofstream variable to write multiple output files

This is a discussion on Using 1 ofstream variable to write multiple output files within the C++ Programming forums, part of the General Programming Boards category; Greeting everyone,
Before you read, I've posted all of the code in the program in case someone wanted to see ...

Using 1 ofstream variable to write multiple output files

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.

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.

The problem is that the input file is opened in main() and then writeOutputFile() loops until it reaches the end of the input file (and produces output in response). On the second and subsequent calls, reading of the input file starts at the end so, after creating the output file, no output is written. Try rewinding the input file before every call of writeOutputFile() (or, alternatively, close it and reopen it, which will mean that reading will start at the beginning of the file).

I suggest avoiding using globals. By doing so, you obfuscated your code ..... that is almost why you thought the problem was with handling of output files, when the real problem is with handling of the input file.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

As a side question, could anyone explain why moving the ifstream pointer back to the beginning of the file didn't work?

Also, CornedBee, could you elaborate on what you meant by "declaring everything up-front only makes it worse. Declare variables at the last possible position." Do you mean that I should declare variables further down in the code? I remember having issues in C if I declared variables after I had code that executed anything.

As a side question, could anyone explain why moving the ifstream pointer back to the beginning of the file didn't work?

Because you probably had a stream error, possibly eof(). Once you get a stream error no further processing will take place on that stream until you clear the error. You could probably have cleared the error then reset the file pointer and been able to read your file correctly. Also note that if you would have just closed the file and reopened it without the clear() you would still have had problems.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Some resets before loop restarts went away now, for example. I also removed some other cleanups (for example, closing files that are about to go out of scope), and did some other cleanups. Note in particular that none of your casts of the result of find() to int are necessary or a good idea by any means.