| | |
java programmer requests help using ofstream
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 5
Reputation:
Solved Threads: 0
I am a java programmer trying to understand a c++ oddity that I ran into. My c++ program writes a few MB of data using ofstream. Just before the program ends I close the ofstream. However when the program terminates, the amount of free memory on my linux box has decreased by the size of the data that was written. The memory remains lost until the file is deleted. Here is a sample of how I use ofstream:
In my constructor, I have:
ofstream tfile("../../tmp/outputfile.txt", std::ios::app);
a number of prints using tfile occur that are similar to the following line:
tfile << track->GetTrackID() << '\t' << track->GetParentID() << '\t' << pName << '\t';
In the destructor, I have:
tfile.close();
Any ideas would be appreciated![I]
In my constructor, I have:
ofstream tfile("../../tmp/outputfile.txt", std::ios::app);
a number of prints using tfile occur that are similar to the following line:
tfile << track->GetTrackID() << '\t' << track->GetParentID() << '\t' << pName << '\t';
In the destructor, I have:
tfile.close();
Any ideas would be appreciated![I]
•
•
•
•
Originally Posted by santa_barbarian
Thanks for the quick answer! How does one typically delete/find the buffer? tfile->delete was my first guess, but it causes segmantation faults since the ofstream constructor was not called...
The problematic snippet of code writes the interesting data from my monte carlo sim.
Alex Cavnar, aka alc6379
I am not a Java person, but in C++ you best look at something like "new" and the potentially missing "delete". Here is a typical code segment (commented to the hilt): [php]
// Allocate memory
buffer = new char[length];
// Read data as a block
is.read(buffer, length);
// Close file
is.close();
// Print data
cout << buffer << endl;
// Free memory
delete []buffer;
[/php]
// Allocate memory
buffer = new char[length];
// Read data as a block
is.read(buffer, length);
// Close file
is.close();
// Print data
cout << buffer << endl;
// Free memory
delete []buffer;
[/php]
May 'the Google' be with you!
•
•
Join Date: Dec 2004
Posts: 5
Reputation:
Solved Threads: 0
I have a RunAction class which has the following global line:
std::ofstream tfile("../../tmp/outputfile.txt", std::ios::app);
RunAction::~RunAction() {
tfile.close();
}
A second class SD has the following global line:
extern ofstream tfile;
SD:
rocessHits(G4Step* step,G4TouchableHistory* ROhist) {
A couple of other classes use tfile in the same way as the SD class.[INDENT]
std::ofstream tfile("../../tmp/outputfile.txt", std::ios::app);
RunAction::~RunAction() {
tfile.close();
}
A second class SD has the following global line:
extern ofstream tfile;
SD:
rocessHits(G4Step* step,G4TouchableHistory* ROhist) {G4Track* track;}
if(step != NULL)
track = step->GetTrack();
else
track = NULL;
G4double edep = 0;
if(step != NULL)
edep = step->GetTotalEnergyDeposit();
bool write = false;
G4int parentID = track->GetParentID();
G4String pName = (track->GetDefinition())->GetParticleName();
G4double kEnergy = track->GetKineticEnergy();
G4double dEnergy = step->GetTotalEnergyDeposit();
G4String volName = track->GetVolume()->GetName();
if((track != NULL) && (step != NULL) && goodEvent) {// Output to text file.}
if((track->GetDefinition()) != NULL)
if((track->GetVolume()) != NULL)
if((step->GetPostStepPoint()) != NULL)
if((step->GetPostStepPoint()->GetProcessDefinedStep()) != NULL)
tfile << track->GetTrackID() << '\t'
<< track->GetParentID() << '\t'
<< pName << '\t'
<< track->GetCurrentStepNumber() << '\t'
<< kEnergy << '\t'
<< dEnergy << '\t'
<< step->GetStepLength() << '\t'
<< track->GetTrackLength() << '\t'
<< track->GetVolume()->GetName() << '\t'
<< step->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName() << '\t'
<< track->GetPosition().x() << '\t'
<< track->GetPosition().y() << '\t'
<< track->GetPosition().z() << '\t'
<< track->GetMomentumDirection().x() << '\t'
<< track->GetMomentumDirection().y() << '\t'
<< track->GetMomentumDirection().z() << '\t'
<< track->GetGlobalTime() << '\t'
<< step->GetPreStepPoint()->GetKineticEnergy() << std::endl;
A couple of other classes use tfile in the same way as the SD class.[INDENT]
![]() |
Similar Threads
- Experienced Java Programmer (Software Development Job Offers)
- WANTED: Freelance Java Programmer (Software Development Job Offers)
- Looking for a java-Programmer (Software Development Job Offers)
Other Threads in the C++ Forum
- Previous Thread: Round Robin Algorithm Simulation
- Next Thread: Pause
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






