ifstream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • serleon
    New Member
    • Mar 2007
    • 3

    #1

    ifstream

    Privet/hello/salut/halo/hola!
    I have the following code:
    _______________ _______________ _______________ _______________ ____
    Code:
    #include "OJFKinematics.h"
    #include "OJFJets.h"
    #include "OJFSearch.h"
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    using namespace OptimalJetFinder;
    
    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...

    Code:
    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..
    file = fopen(name,"r");
    fclose(name);
    thks
    Last edited by Ganon11; Mar 6 '07, 01:05 PM. Reason: code tags added
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    the method takes a String as input...and we can (reasonably easily) turn digits ito strings, so if you set up a loop incrementing som var (let's say i), all you need to do is create a string that takes your variable i (no wuotes) appends ".dat", and then you pass this new string through to the unction....

    Comment

    • serleon
      New Member
      • Mar 2007
      • 3

      #3
      All right, thanks... that is what I am trying to do, but it doesn't work...
      Maybe something wrong in the code:

      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 line is all right,
      any idea of how making it work?

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        I'm pretty sure that c (and c++ more so) is quite clever, and if you say (for example):

        name = i +".dat"; //that is don't have the i in quotes

        or You may need an empty string first as in

        name = "" + i + ".dat";

        if this doesn't work, you could try to implement a method that converts i to it's equivalent char value and concatenate that, or something similar....

        Comment

        • serleon
          New Member
          • Mar 2007
          • 3

          #5
          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 (any idea?)

          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?

          Comment

          Working...