open a thousand .dat files for input operations

Hi!
I would like a piece of advice about a program that I'm writing:
First part (that I can manage myself):
I've to call an input file.dat, a column of int numbers, find the maximum int among them and write it in another .dat file.
Second part:
I've to repeat the first part with other thousand input file of the same kind and print each found maximum in sequence in the same output dat file.
My input files have the same root and they're numbered from one to 1000.
I've tried to use that in my favor but so far my loops had been unsuccessful.
Suggestions are welcome:)

considering you are using std::, ios::out should also be std::ios::out unless your compiler is ignoring it

the problem is how youre calling the stringstream.
the loop executes fine but it never opens the files due to incorrect naming.

after calling std::stringstream ss(rootName); when you insert new strings into it, they all ram up at the beginning and start erasing "auto_200_" so you need to add this after it;ss.seekp(0, std::ios_base::end);
this tells the stringstream you want to append things to the end of it.
let me make an example. running the code you have there. because of how you called the stringstream, it would try to open a file called this 1.dat200_ all the way up to 999

another suggestion is calling fileinput.close(); at the end of the loop.

also is that your whole loop? the fstream fileoutput never gets called. enjoy :-)

The first time through the loop this would not be necessary, but on all additional passes through the loop it is. This is because after reading in the input file the failbit will be set because of hitting end of file. The next time through the loop the call to fileinput.open() will fail unless you first clear the stream. The call to fileinput.close() at the bottom of the loop simply closes the stream but does not clear the failed state.

You get no output results because your files are suppose to be 1-1000 but your loop runs from 0 to 999. Consequently, the first time through the loop the call to fileinput.open() will fail because the file 0.dat does not exist. The remaining times through the loop fail because of not clearing the stream as described above. Change your for loop to:

ffor(int i = 1; i<=1000; i++)

Also, since you are only reading the input files and only writing to the output file, I would use ifstream and ofstream instead of fstream. That is,

sorry for the late reply.
the program i wrote is a bit redundant but the problem is that if i call the fstream fileoutput inside the for loop (including the if statement) then i get ./a.out :
*** glibc detected *** ./a.out: double free or corruption (out):(...).
Else i have only one correct output ( the first maximum).
here the program with some of your suggestions:
(in this condition i get from it the first correct value)

(I'm sorry to bother again)
Maintaining the call of the fstream output outside the loop is simply the only way in which i get at least one of my values, otherwise, from
(the beginning is still the same a part from std::ifstream fileinput; that i put inside the for loop)

I don't know why you keep clumping the input and output files together, they are different objects and you are using them differently. You want the output for the 1000 files to go into 1 file, so the output stream needs to exist for most of the processing. The input file though, you only need 1 open at a time per iteration so there is no point putting it with the output file as it has different conditions for when it needs to be opened. Just seems like you are guessing at this point instead of thinking it through.

p.s. the program (finally) works,
the problem was the memory freeing (that needed to be done at the beginning of the if statement to consent the following for loop to iterate).
thank you all for your precious help and patience.