473,516 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream & string

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
3 4290
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13747
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 the easiest way to take a huge project across onto it. Basically, I've hit a problem. I have no idea how the ifstream class handles directories....
6
3626
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" file_names = "file2.txt" etc ...
6
3371
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" 10,20,30
10
17900
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");
0
7581
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5714
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.