Hey i have 1000 replicate files with a file name looks like below
a.out1
a.out2
a.out3
.....
a.out1000

each file contains 12 rows and 12 columns including the header.
a.out1 looks like this
n col1 col2 col3 col4 ...........col12
1 0.3 0.1 0.1 0.11............0.5
2 ....................................
3
4
5
6
7
8
9
10
11
12

now how can i get the average of the 100 files and print it in to one file called a.out
a.out.

thanks

Should be not very complicated. Just read the files in a loop

#include <string>
#include <fstream>
#include <sstream>

int main()
{
   std::ifstream in;
   for(int i = 1;  <= 100; i++)
   {
       std::stringstream s;
       s << i;
       std::string filename = "a.out";
       filename += s.str();
       in.open(filename.c_str());
       if( in.is_open() )
       {
          std::string line;
          while( std::getline(in,line) )
          {
             // do stuff here
          }
          in.close();
          in.clear(); // clear eof errors
       }
     }
}

Edited 6 Years Ago by Ancient Dragon: n/a

Yeah! Thanks for the Hint. but it complains in ine 30:line >> y >> yy >> yyy; any help please :)

#include <iostream>
#include <fstream>
#include <sstream>


using namespace std;

int main()
{
    ifstream in ;
    for(int i=1; i<=100; ++i)
    {
        stringstream s;
        s<<i;
        string filename="a.out";
        filename+=s.str();
        in.open(filename.c_str());
        int count=0;
        if(in.is_open())
        {
            string line;
            double sum_y;
            double sum_yy;
            double sum_yyy;
            int y;
            double yy, yyy;
            while(getline(in,line))
            {
                count++;
                line >> y >> yy >> yyy;
                sum_y+=y;
                sum_yy+=yy;
                sum_yyy+=yyy;
            }
            in.close();
            in.clear();
            double av_y=sum_y/count;
            double av_yy=sum_yy/count;
            double  av_yyy=sum_yyy/count;
            cout<< av_y  << "    "
                << av_yy << "    "
                << av_yy << "    " << endl;
        }

    }
    return 0;
}

Edited 6 Years Ago by blessedboy123: n/a

I just made some improvements. of course it works but my output becomes full of
INF INF INF 100 times :-)

#include <iostream>
#include <fstream>
#include <sstream>


using namespace std;

int main()
{
    ifstream in ;
    for(int i=1; i<=100; ++i)
    {
        stringstream s;
        s<<i;
        string filename="a.out";
        filename+=s.str();
        in.open(filename.c_str());
       int count=0;
        if(in.is_open())
        {
            string line;
            double sum_y;
            double sum_yy;
            double sum_yyy;
            int y;
            double yy, yyy;
            while(in >> y >> yy >> yyy)
            {
                count++;

                sum_y+=y;
                sum_yy+=yy;
                sum_yyy+=yyy;
            }
            in.close();
            in.clear();
            double av_y=sum_y/count;
            double av_yy=sum_yy/count;
            double  av_yyy=sum_yyy/count;
            cout<< av_y  << "    "
                << av_yy << "    "
                << av_yy << "    " << endl;
        }

    }
    return 0;
}
This question has already been answered. Start a new discussion instead.