473,396 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ifstream

Privet/hello/salut/halo/hola!
I have the following code:
__________________________________________________ ______________
Expand|Select|Wrap|Line Numbers
  1. #include "OJFKinematics.h"
  2. #include "OJFJets.h"
  3. #include "OJFSearch.h"
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <fstream>
  7. #include <cstdlib>
  8. using namespace std;
  9. using namespace OptimalJetFinder;
  10.  
  11. int main() {
  12.   ifstream in( "1.dat" );
  13.   Event *P = new Event( sphere );
  14.   double px, py, pz;
  15.   while( in>>px>>py>>pz ) {
  16.     P->AddParticleRaw( px, py, pz ); 
  17.   }
  18.   in.close();
  19.  
  20.   P->Normalize();
  21.   cout << P->GetNumber() << " particles in the event." << endl;
  22.   OJFRandom::SetSeed( 13 );
  23.   double radius = 1.0; // R parameter of eq. (20) in reference [1]
  24.   unsigned ntries = 3; // number of tries
  25.   JetSearch* js = new JetSearch( P, radius, ntries );
  26.   unsigned njets = js->FindJetsForOmegaCut(0.05);
  27.   if( njets == 0 ) { cout << "Jets lost." << endl; exit(1); }
  28.   Jets* Q = js->GetJets();
  29.   cout << Q->GetNumber() << " jets found." << endl;
  30.   cout << "Omega: " << Q->GetOmega() << ",   "
  31.        << "Y: " << Q->GetY() << ",   "
  32.        << "Esoft (normalized): " << Q->GetESoft() << "." << endl;
  33.   cout << "The details of the jets (E px py pz):" << endl;
  34.   Jet* jet = Q->GetFirst(); 
  35.   while( jet ) {
  36.     cout << setw( 10 ) << jet->GetE() << "  "
  37.      << setw( 10 ) << jet->GetPx() << "  "
  38.      << setw( 10 ) << jet->GetPy() << "  "
  39.      << setw( 10 ) << jet->GetPz() << endl;
  40.     jet = jet->GetNext();
  41.   }
  42.   delete P;
  43.   delete js;
  44. }
__________________________________________________ _____________

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

Expand|Select|Wrap|Line Numbers
  1. string name[6]
  2. for (i=o; i<h;i++);
  3. name = "i+.dat";  --- trying to open "1.dat", runnig it, then open "2.dat" and so..
  4. file = fopen(name,"r");
  5. fclose(name);
thks
Mar 6 '07 #1
4 1506
DeMan
1,806 1GB
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....
Mar 6 '07 #2
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?
Mar 7 '07 #3
DeMan
1,806 1GB
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....
Mar 7 '07 #4
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?
Mar 7 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is...
7
by: Anton Ishmurzin | last post by:
Greetings All, I think everybodyknows the answer already. But i am quite a newbie in c++. I've got the following line in my code: ifstream ini_file_in("filename.dat", ios::in); But, the...
6
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt"...
6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
4
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
1
by: Xiaozhou.Yin | last post by:
Hi~ In the program,I first used the ifstream variable fin open the file and,open it again after called the fin.close().But the fin is fieled to open the file in the second time.The book I'm...
11
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream...
5
by: DrSchwartz | last post by:
Hi there, I have to read in from file, and then process it. It looks like this. #include <iostream> #include <string> #include <fstream> using namespace std; ... int main() {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.