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
       }
     }
}

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;
}

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;
}

>> output becomes full of INF INF INF 100 times :-)

You have to initialize the sum_* variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.