Output to text file

closed account (zbREy60M)
First off, thanks for helping.

My problem is that I am trying to output the coordinates that I have generated into a .txt file. I copied the code for the text dump straight from this website but I am only getting a single line of code. When I run the program it asks me how many "Gravitars" (coordinates) I want to produce. I then select an arbitrary number such as 5, and I see all the coordinates on the screen. But when I open the data.txt the only coordinate in the file is the last one. It is almost as if it overwrites the old data everytime it runs the loop. Thanks for the help!

Cheers!
*edit, the part that is of concern is at the very end lines 97-103


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>

using namespace std;
using std::vector;

double unifRand()// Generate a random number between 0 and 1
{
return rand() / double(RAND_MAX);// return a uniform number in [0,1].
}
double unifRand(double a, double b) // Generate a random number in a real interval.
{ // param a one end point of the interval; param b the other end of the interval 
return (b-a)*unifRand() + a;// return a inform rand numberin [a,b].
}
long unifRand(long n) // Generate a random integer between 1 and a given value.
{
if (n < 0) n = -n;  // param n the largest value; return a uniform random value in [1,...,n]
if (n==0) return 0; //Makes sure that n == 0
long guard = (long) (unifRand() * n) +1; //So that there cannot be a return value of n+1
return (guard > n)? n : guard;
}
void seed() // Reset the random number generator with the system clock.
{
srand(time(0));
}
class Gravitar //creates a class called Gravitar
{
public: //public assesors
	double GetRadius();
	void SetRadius(double radius);
	double GetAngle();
	void SetAngle(double angle);
	double GetZed();
	void SetZed(double zed);
private: //private values
	double Radius;
	double Angle;
	double Zed;
};
double Gravitar::GetRadius() //GetRadius, public assessor function returns value of Radius
{
	return Radius; //returns the value Radius
}
double Gravitar::GetAngle()
{
	return Angle; //returns the value of Angle
}
double Gravitar::GetZed() 
{
	return Zed;
}
void Gravitar::SetRadius(double radius)//sets radius to Radius
{
	Radius = radius;
}
void Gravitar::SetAngle(double angle)
{
	Angle = angle;
}
void Gravitar::SetZed(double zed)
{
	Zed = zed;
}
int main()
{
seed();
int m ;
cout << "How many Gravitars?\n\n";
cin >> m;
double R;
double A;
double Z;
for (R = 0; R < m; ++R)
{	
	double radius = unifRand();
	double angle = unifRand();
	double zed = unifRand();
Gravitar RadialComponents;
RadialComponents.SetRadius(radius);
Gravitar AngularComponents;
AngularComponents.SetAngle(angle);
Gravitar ZedComponents;
ZedComponents.SetZed(zed);
double x = RadialComponents.GetRadius()*cos (AngularComponents.GetAngle()*6.283185307179586476925286766559)*50000;
double y = RadialComponents.GetRadius()*sin (AngularComponents.GetAngle()*6.283185307179586476925286766559)*50000;
double z = ZedComponents.GetZed()*500;
cout  << "Gravitar #" << R << " (" << x << "," << y << "," << z << ")" << endl;
ofstream myfile ("data.txt");
if (myfile.is_open())
{
	myfile << "Gravitar #" << R << " (" << x << "," << y << "," << z << ")" << endl;
	myfile.close();
}
}
system("PAUSE");
return 0;
}
Last edited on

It is almost as if it overwrites the old data everytime it runs the loop.


That is actually exactly what is happening. Each time you go through the loop, you open the file, close it, then open it again in the next loop. When you open a file without specifying that the contents should not be erased, everything in the file is erased. There is a way to open a file and just add to the end of it, but in this case you can just open the file outside the loop, then close it after the loop runs. That should fix your problem.

Edit: I just ran the program myself, once without changing your code and then again after I did what I said. It now writes all the data to the file without erasing anything.
Last edited on
closed account (zbREy60M)
Thank you so much!
Topic archived. No new replies allowed.