I have the following code:

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <cstdlib> 
using namespace std; 
using namespace Finder; 

int main() { 
 ifstream in( "1.dat" ); 
 Event *P = new Event( sphere ); 
 double px, py, pz; 
 while( in>>px>>py>>pz ) { 
   P->AddParticleRaw( px, py, pz ); 

 cout << P->GetNumber() << " particles in the event." << endl; 
 OJFRandom::SetSeed( 13 ); 
 double radius = 1.0; // R parameter of eq. (20) in reference [1] 
 unsigned ntries = 3; // number of tries 
 JetSearch* js = new JetSearch( P, radius, ntries ); 
 unsigned njets = js->FindJetsForOmegaCut(0.05); 
 if( njets == 0 ) { cout << "Jets lost." << endl; exit(1); } 
 Jets* Q = js->GetJets(); 
 cout << Q->GetNumber() << " jets found." << endl; 
 cout << "Omega: " << Q->GetOmega() << ",   " 
      << "Y: " << Q->GetY() << ",   " 
      << "Esoft (normalized): " << Q->GetESoft() << "." << endl; 
 cout << "The details of the jets (E px py pz):" << endl; 
 Jet* jet = Q->GetFirst(); 
 while( jet ) { 
   cout << setw( 10 ) << jet->GetE() << "  " 
    << setw( 10 ) << jet->GetPx() << "  " 
    << setw( 10 ) << jet->GetPy() << "  " 
    << setw( 10 ) << jet->GetPz() << endl; 
   jet = jet->GetNext(); 
 delete P; 
 delete js; 


My little practical question is about opening using "ifstream":
I have a directory with some "n.dat" (n=1,2...) files that are used in the code, so just wanted the program to compile all these files one by one.
How to make the program to open not just one file "1.dat", but to open
the next file after compiling this one, for example beginning with
"1.dat" and ending at "10.dat" compiling all of them, and making a
final file with all the data obtained?
I made this code to do that, but unfortunately it doesn't work...

for(int i = 1; i <= 10; i++)
   std::stringstream ss;
   ss << i << ".dat";


9 Years
Discussion Span
Last Post by StuXYZ

I made this code to do that, but unfortunately it doesn't work...

for(int i = 1; i <= 10; i++)
   std::stringstream ss;
   ss << i << ".dat";


I think you're pretty close, actually. The problem is that you're never taking the string off of the stringstream. And since it goes out of scope at the end of the loop, you'll not get a chance later either. Just rough sketch, but what about something like this:

vector<string> filenames;
stringstream ss;
for(int i = 1; i <= 10; i++)
  ss << i << ".dat";
  string temp;
  ss >> temp;
// then read through the files...

btw, welcome to the forums. While your post was generally well done, please do familiarize yourself with the rules (e.g. code tags). :)

Edited by mike_2000_17: Fixed formatting


The way I see it to open a file with a different name you can use something like this:

 int main()
     for (int number=1;number<=10; number++)
          int number=1;
          ifstream file;
          file.open((char) number+48 ".dat",ios::in etc);
          //48 is the charactor for 0, so number+48 is the charactor for 
          //that number. 10 is not on there so you might have to do
          //something else for that
          (do stuff with open file)

another idea is to make a char array with 10 elements and do something like:

char array[10];

and then open the file with array+".dat"


First off welcome. I think this looks like particle physics code :-)
In particular Cern style code. Looks like you are going to have fun.

I am going to disagree with Swot since it doesn't compile.
You can't add a char to a char array.

Sorry, I think you are also wrong since your are not clearing the flag state of the stringstream. So you will get "1.dat", empty string, empty string

So this is my attempt

std::vector<std::string> Filenames; 
std::ostringstream ss;
for(int i = 1; i <= 10; i++)
      ss << i << ".dat";

Finally, stream object DO NOT TAKE strings, into open, you need this (example):

std::string temp="afilename.dat";
std::ifstream X(temp.c_str());

Infraction did say it was a rough idea, not the exact code to use. Also the ifstream takes C Strings, the function c_str() isfound in the string header and converts string object to C Strings, incase you were not aware of this.



I do not know anything about streams in the way you are talking about, and I guess I should have tried to compile what I wrote before I wrote it....
What I was trying to say was that you can use char*or char[] for the ifstream's open function and that therefore the name of the file you are opening is not set. This means with some experimentation you can open files with simiular names in a loop, i.e. file1.txt file2.txt etc.
This way you can open your 10 files without having to copy and paste the code you have 10 times.


Sorry to all, who I seem to upset. I should have just suggested here is another way to do it.

I have two comments (in my not very humble defense):

Infraction's error is a horrible thing to debug. You somehow have to know that ss >> temp sets the eof flag (assuming it consumes all the string). So you have to know that you need a ss.clear() afterwards, or do it differently. A journey-man programmer, can easily spend a long time trying to find that out. Nearly working but leading the questioner down into such difficult language/library issues is worthy of pointing out.

Swot, sorry, I was a bit flippant but the problem will be that to avoid an extra inner loop you have to head down to stdlib stuff or use streams. (or fancy stuff.). you are correct that ifstream needs a char* but it needs yet more to get confused with. (especially since you can get a char* from a string with one method).

Overall the questioner (serleon) has (a) put some effort in, (b) is obviously comes from a field were his main expertise are not C++,
but requires serious ability.

This deserves a level of respect above that we typically see, i.e. "I am doing a course on c++ and I want my homework debugged". I just wanted him to see something working.

So overall, sorry if my tone was a bit flippant. I didn't mean to offend so my apologies to all. I will be more contrite next time.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.