By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

ifstream & string

P: n/a
Leo
Privet/hello/salut/halo/hola!
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 );
}
in.close();

P->Normalize();
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":

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

string name[6]
for (i=o; i<h;i++);
name = "i+.dat";
--- trying to open "1.dat", runnig it, then open "2.dat" and so..
but i'm not sure if the second/third line is all right,
any idea of how making it work?

Mar 7 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Leo wrote:
Privet/hello/salut/halo/hola!
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 );
}
in.close();

P->Normalize();
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":

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

string name[6]
for (i=o; i<h;i++);
name = "i+.dat";
--- trying to open "1.dat", runnig it, then open "2.dat" and so..
but i'm not sure if the second/third line is all right,
any idea of how making it work?
Here's one way using the C function sprintf

for (i = 1; i < 10; i++)
{
char name[99];
sprintf(name, "%d.dat", i);
// do something with name
...
}

Hope this is something like what you need.

john
Mar 7 '07 #2

P: n/a
Leo
Thanks! Will try that one!
ouh.. a trouble, the program executes just the last opened file, and
as i understand, rewrites all the previous results...
hmmm

Another little trouble: i don't know, but maybe is easier (what was
trying to do now) just to open the directory where all my files
"n.dat" (n=1,2...), and make c++ to run them one by one?

Mar 7 '07 #3

P: n/a

"Leo" <ls*****@gmail.comwrote in message
news:11**********************@c51g2000cwc.googlegr oups.com...
Privet/hello/salut/halo/hola!

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

string name[6]
for (i=o; i<h;i++);
{
std::ostringstream oss;
oss << i << ".dat";
std::ifstream in(oss.str().c_str());
/* etc */
}

'std::ostringstream' is declared by standard header <sstream>.

-Mike
name = "i+.dat";
--- trying to open "1.dat", runnig it, then open "2.dat" and so..

Mar 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.