jeudi 10 août 2017

C++: changing output file inside IF statement

Here I show a snippet of my C++ code which loops over some k values and if k=0 should send the output to one file (outputk0.txt), if k!=0 to another one (outputnotk0). I am having a problem with the scope of the out ofstream file when compiling. Anyone knows how to fix this?

for(int k = 0; k < number_of_l_steps; k++)
  {

    if(k == 0)
      std::ofstream out("outputk0.txt", ios::out);
    else
      std::ofstream out("outputnotk0.txt", ios::out);

    for(int i = 0; i < n_k; i++)
    {
      cout << i << endl;
      double k = exp( log(kmin1) + i*(log(kmax) - log(kmin1))/(n_k-1.));
      for(int j = 0; j < n_z; j++)
      {
         double z = zmin + j*(zmax - zmin)/(n_z-1.);
      //  cout << k << " " << z << endl;
        out << i << " " <<  j <<  " " << cv.U(z,k) << endl;
        //out << i << " " << j << " " << gsl_matrix_get(Gl,i,j) << endl;
      }
    }
    out.close();
  }

Aucun commentaire:

Enregistrer un commentaire