trouble with fstream and for loop

Hi I was trying to do a program that would allow me to elaborate datas coming from moultiple files at once, so I used fstream( or ofstream if you prefer) in a for loop, but it does give me only one output file, even if I declare I want n numbers in output, here's the program, P.S of course right now it is useless because I just take data from a file and transfer it to another, but if it works I can obviously do whatever I want from the data I collect...

#include <iostream>
#include<cmath>
#include<fstream>
#include<sstream>
#include<string>
#include <iomanip>
using namespace std;
string convertInt(int);
int main ()

{
cout << "insert the name of the files to elaborate ( no numbers no txt" << endl;
string namefile;
cin >> namefile;
cout << "insert the number of files to elaborate" << endl;
int nfile;
cin >> nfile;
cout <<"insert the numbers of elements"<< endl;
int nel;
cin >> nel;

cout << " insert the name of the files where to save your new data" << endl;
string fileout;
cin >> fileout;
fstream datin;
fstream datout;
double *arr1= new double [nel];
for ( int i=1; i <= nel; i++)
{
string a = namefile.c_str() + convertInt(i) + ".txt" ;
datin.open (a.c_str() ,fstream :: in);
for( int j = 0; j < nel; j++)
{
datin >> arr1[j];
}
string b= fileout.c_str() + convertInt(i) + ".txt" ;
datout.open( b.c_str() , fstream :: out );
for ( int j =0; j < nel ; j++)
{
datout << arr1[j]<<endl;
}
}
datin.close();
datout.close();
delete [] arr1;
return 0;
}

string convertInt(int number)
{
stringstream n;
n << number;
return n.str();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for ( int i=1; i <= nfile; ++i )
{
  const string a = namefile + convertInt(i) + ".txt" ;
  const string b = fileout + convertInt(i) + ".txt" ;

  std::ifstream datin( a );
  std::ofstream datout( b );

  size_t count = 0;
  double value = 0.0;
  while ( count < nel && datin >> value )
    {
      datout << value;
      ++count;
    }
}
Topic archived. No new replies allowed.