Looping over files in C++ and changing their names to store data into a single file I have a lot of data in separate files generated from a simulation program. All data is clear and formatted via a single space to read in 4 columns. I can open and store data from each file one at a time, but that will get so impossible once I hit more than, say, 10 files of data with the same simulation data columns.

I am just trying to find a better way to open files and store the data using an iterative method instead of typing, for example, "open file, read file data, append to file" over 1000 times.

#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt;
#include &lt;vector&gt;

using namespace std;

int main ()
{
// INITIALIZE
    int tmax = 80000;

    float time_step[tmax];

    float LEx[tmax];
    float LEy[tmax];
    float LEz[tmax];
    float LE[tmax];

    float AvgLEx;
    float AvgLEy;
    float AvgLEz;
    float AvgLE;

    float Function[tmax];

// FILES
    ifstream infile;
    infile.open("File_1.prn");
// THIS INFILE is where I want to put a loop to change File_1 to File_2 to File_3 and so on and so on to append data from all those files into ONE single file
    if(infile.fail())
    {
        cout &lt;&lt; "error reading file" &lt;&lt; endl;
        return 1;
    }

    for (int i = 0; i &lt; tmax+1; ++i)
    {
        infile &gt;&gt; time_step[i] &gt;&gt; LEx[i] &gt;&gt; LEy[i] &gt;&gt; LEz[i] &gt;&gt; LE[i];
        AvgLEx = AvgLEx + LEx[i];
        AvgLEy = AvgLEy + LEy[i];
        AvgLEz = AvgLEz + LEz[i];
        AvgLE = AvgLE + LE[i];

    }

    AvgLEx = AvgLEx/tmax;
    AvgLEy = AvgLEy/tmax;
    AvgLEz = AvgLEz/tmax;
    AvgLE = AvgLE/tmax;

    cout &lt;&lt; "AvgLE: " &lt;&lt; AvgLE &lt;&lt; "\n";
    cout &lt;&lt; time_step[tmax] &lt;&lt; " LEx " &lt;&lt; LEx[tmax] &lt;&lt; " LEy " &lt;&lt; LEy[tmax] &lt;&lt; " LEz: " &lt;&lt; LEz[tmax] &lt;&lt; " LE: " &lt;&lt; LE[tmax] &lt;&lt; endl;

    cout &lt;&lt; "\n";

    infile.close();
    cout &lt;&lt; "\nFile read! \n";

// FUNCTIONS

    for (int i = 0; i &lt; tmax+1; ++i)
    {
        Function[i] = LE[i] - AvgLE;
        cout &lt;&lt; LE[i] &lt;&lt; " " &lt;&lt; AvgLE &lt;&lt; " " &lt;&lt; Function[i] &lt;&lt;  "\n";
    }

    cout &lt;&lt; "\n Function set up \n";

// // CONVOLUTION
    float A_t;
    float A_dt;
    int dt, t;

    float max=0;

        for (int dt = 0; dt &lt; 1 + tmax/10; ++dt)
        {
            //dt = 1;
            cout &lt;&lt; "dt: " &lt;&lt; dt &lt;&lt; "\n";
            //A_dt = 0;
            int window = tmax-dt;
            for (int t = 0; t &lt; window; ++t)
            {
                if (t==0)
                {
                    A_t = 0;
                    cout &lt;&lt; "A_t was set to zero \n";
                }

                A_t+= Function[t] * Function[t + dt];
                cout &lt;&lt; "A_t is accumulation ...: " &lt;&lt; A_t &lt;&lt; "\n" ;
            }

            A_dt = A_t / window;

            if (A_dt &gt; max)
            {
                max = A_dt;
            }

// OUTPUT
            ofstream AutoCor;
            AutoCor.open ("AutoCor_25.prn", ios::app);
            AutoCor &lt;&lt; dt &lt;&lt; " " &lt;&lt; A_dt &lt;&lt; "\n";
            AutoCor.close();
            cout &lt;&lt; "A_dt: " &lt;&lt; A_dt &lt;&lt; " " &lt;&lt; "window used: " &lt;&lt; window &lt;&lt; "\n";

        }

        cout &lt;&lt; "max used: " &lt;&lt; max &lt;&lt; "\n";

// RETURN       
    return A_dt;
    return 0;

}
As you're new to C++, you should be aware that implementing an entire program in main() can make things difficult, especially when the code is already complex.

As it appears, you have declare and initialize variables at the top, open the file, perform the operations, then output things.  Even with these comments in place, you should still split this up into functions.


Do not maintain a list of variables as it could make maintenance more difficult, such as if variables are no longer being used.  Instead. you should only declare or initialize variables as close to their intended use as possible.  That way, if you no longer use that variable, you won't have to search for it at the top.  This will also make your code easier to follow.
Consider just having main() do two things: attempt to open the file and call all the necessary functions afterwards.  You can also open the file in an other function, but if you do it in main(), then you can easily return from there if the file cannot be opened.
Everything else, especially the file operations, should be in separate functions.  Also keep in mind that a function should just have one primary purpose, in order to maintain the Single Responsible Principle (SRP).  If a function needs to do something else that another function already does, then it should call that function.  You should also name functions, in the verb form, based on what they're supposed to do.  This will make the function's intent very clear to others.

Example:

void openFile() {}



Miscellaneous:


Try not to use using namespace std in global scope as it could cause name-clashing bugs, especially in larger programs.  See this post for more info on this.
This variable appears to be a constant:


int tmax = 80000;



If so, initialize it with const so that it cannot be modified:

const int tmax = 80000;

For this approach at checking the file:


infile.open("File_1.prn");

if(infile.fail())
{
    cout &lt;&lt; "error reading file" &lt;&lt; endl;
    return 1;
}



you should use std::ifstream, check for !infile(), print the error message to std::cerr, and then return EXIT_FAILURE:

std::ifstream infile("File_1.prn");

if (!infile)
{
    std::cerr &lt;&lt; "error reading file";
    return EXIT_FAILURE;
}