Creating a text document.

How would i take all the data from my c++ application and put it into a log file?

or make a text document for it =D
closed account (3hM2Nwbp)
You will need to give much more information in order to get a less generic solution.

http://cplusplus.com/reference/iostream/fstream/
1
2
3
4
5
6
7
8
#include <cstdio>

int main(){
	FILE* file=fopen("log.txt","a");
	//use functions like fwrite, fputc, fputs, etc.
	fclose(file);
	return 0;}
Last edited on
putting stuff like
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
	cout << "INTRODUCTION\n";
	cout << Name << ", " << Class << endl << endl;
	cout << "Strength: " << Strength << endl;
	cout << "Agility: " << Agility << endl;
	cout << "Intelligence: " << Intelligence << endl << endl;
	cout << "Learns Skills: " << Skill[0] << endl << endl;
	cout << "Attack Range of: " << AttackRange << endl;
	cout << "Movement Speed of: " << Movespeed << endl << endl;
	cout << "HERO INFORMATION" << endl << endl;
	cout << "Affiliation: " << Affiliation << endl;
	cout << "Role: " << Role << endl << endl;
	cout << "HERO ABILTIES" << endl;
	cout << "Skill 1" << endl;
	cout << Skill[1] <<endl;
	cout << "Level 1 - " << SK1[0] << endl;
	cout << "Level 2 - " << SK1[1] << endl;
	cout << "Level 3 - " << SK1[2] << endl;
	cout << "Level 4 - " << SK1[3] << endl;
	cout << "Level 5 - " << SK1[4] << endl;
    cout << "Range: " << Range[0] << endl;
    cout << "Duration: " << Duration[0] << endl;
    cout << "Cooldown: " << Cooldown[0] << endl;
    cout << "Mana Cost: " << ManaCost[0] << endl << endl << endl;
    cout << "Skill 2" << endl;
    cout << Skill [2] << endl;
    cout << "Level 1 - " << SK2[0] << endl;
    cout << "Level 2 - " << SK2[1] << endl;
    cout << "Level 3 - " << SK2[2] << endl;
    cout << "Level 4 - " << SK2[3] << endl;
    cout << "Level 5 - " << SK2[4] << endl;
    cout << "Range: " << Range[1] << endl;
    cout << "Duration: " << Duration[1] << endl;
    cout << "Cooldown: " << Cooldown[1] << endl;
    cout << "Mana Cost: " << ManaCost[1] << endl << endl << endl;


into a text document =P
the console output part, not the actual code.
Maybe you could make a class that combines iostream with fstream:

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
#include <iostream>
#include <fstream>

using namespace std;

class io{
	private:
		fstream file;
	public:
		io(const char* fname){
			open(fname);}
		~io(){
			if(file.is_open()){
				file.close();}}
		void open(const char* fname){
			file.open(fname,fstream::app);}
		void close(){
			file.close();}
		template<typename T>
		io& operator<<(T& value){
			if(file.is_open()){
				file<<value;}
			cout<<value;
			return *this;}
		template<typename T>
		io& operator>>(T& value){
			cin>>value;
			return *this;}};

Just use the io class to input/output anything you need.
PiMaster where would it save the txt file to?
Whatever file name you give it. For instance:

1
2
3
4
5
6
int main(){
	io rpgio;
	rpgio.open("log.txt");
	//use input/output
	return 0;}

In the above case, everything would be appended to "log.txt"
Sorry for all the hassle from this i literally watch youtube tutorials to learn this until i go to college for it -_-

So im putting the int main() part on the bottom of my code or on top?

just need a small example of how...i dont want you to do everything for me or i wont learn anything =(
Last edited on
closed account (3hM2Nwbp)
Writing to a file is similar in syntax to using cout, only your output is a file. At this point you might want to skip over understanding PiMaster's operator overloads dealing with templates and just use the class for what it is. He's posted a usage example two posts up.

1
2
3
4
5
6
7
int main(){
        int someInt = 0;
	io rpgio;
	rpgio.open("log.txt");
	//use input/output
        rpgio << "someInt: " << someInt << '\n'; //<-- i/o example - almost like cout :)
	return 0;}
Says no default constructor exists for rpgio, also i have no idea what a constructor is, as i said im like bad at c++ atm =D
closed account (3hM2Nwbp)
The constructor takes a const char* as an argument, so...
 
io rpgio("somename.txt");
Its creating the text document but its not writing anything in it, am i doing something wrong?
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
	
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>

using namespace std;

class io{
	private:
			fstream file;
	public:
			io(const char* fname){
				open(fname);}
			~io(){
				if(file.is_open()){
					file.close();}}
			void open(const char* fname){
				file.open(fname,fstream::app);}
			void close(){
				file.close();}
			template<typename T>
			io& operator<<(T& value){
				if(file.is_open()){
					file<<value;}
				cout<<value;
				return *this;}
			template<typename T>
			io& operator>>(T& value){
				cin>>value;
				return *this;}};


int main()
{
	io rpgio("something.txt");
	rpgio.open("log.txt");

	string Name;
	string Class;
	string Strength;
	string Agility;
	string Intelligence;
	string Skill[5];
	string AttackRange;
	string Movespeed;
	string Affiliation;
	string Role;
	string SK1[5];
	string SK2[5];
	string SK3[5];
	string SK4[5];
	string Range[4];
	string Duration[4];
	string Cooldown[4];
	string ManaCost[4];

	cout << "Summon name?: ";
	getline(cin, Name);

	cout << "Class?: ";
	getline(cin, Class);

	cout << "Strength?:  ";
	getline(cin, Strength);

	cout << "Agility?: ";
	getline(cin, Agility);

	cout << "Intelligence?: ";
	getline(cin, Intelligence);

	cout << "Learns Skills?: ";
    getline(cin, Skill[0]);

	cout << "Attack Range?: ";
	getline(cin, AttackRange);

	cout << "Movement Speed?: ";
	getline(cin, Movespeed);

	cout << "What is his Affiliation?: ";
	getline(cin, Affiliation);

	cout << "What is his role?: ";
	getline(cin, Role);

	cout << "Skill 1 Description: ";
	getline(cin, Skill[1]);

	cout << "Level 1: ";
	getline(cin, SK1[0]);

	cout << "Level 2: ";
	getline(cin, SK1[1]);

	cout << "Level 3: ";
	getline(cin, SK1[2]);

	cout << "Level 4: ";
	getline(cin, SK1[3]);

	cout << "Level 5: ";
	getline(cin, SK1[4]);

	cout << "Range: ";
	getline(cin, Range[0]);

	cout << "Duration: ";
	getline(cin, Duration[0]);

	cout << "Cooldown: ";
	getline(cin, Cooldown[0]);

	cout << "Mana Cost: ";
	getline(cin, ManaCost[0]);

	cout << "Skill 2 Description: ";
	getline(cin, Skill[2]);

	cout << "Level 1: ";
	getline(cin, SK2[0]);

	cout << "Level 2: ";
	getline(cin, SK2[1]);

	cout << "Level 3: ";
	getline(cin, SK2[2]);

	cout << "Level 4: ";
	getline(cin, SK2[3]);

	cout << "Level 5: ";
	getline(cin, SK2[4]);

	cout << "Range: ";
	getline(cin, Range[1]);

	cout << "Duration: ";
	getline(cin, Duration[1]);

	cout << "Cooldown: ";
	getline(cin, Cooldown[1]);

	cout << "Mana Cost: ";
	getline(cin, ManaCost[1]);

	cout << "Skill 3 Description: ";
	getline(cin, Skill[2]);

	cout << "Level 1: ";
	getline(cin, SK3[0]);

	cout << "Level 2: ";
	getline(cin, SK3[1]);

	cout << "Level 3: ";
	getline(cin, SK3[2]);

	cout << "Level 4: ";
	getline(cin, SK3[3]);

	cout << "Level 5: ";
	getline(cin, SK3[4]);

	cout << "Range: ";
	getline(cin, Range[2]);

	cout << "Duration: ";
	getline(cin, Duration[2]);

	cout << "Cooldown: ";
	getline(cin, Cooldown[2]);

	cout << "Mana Cost: ";
	getline(cin, ManaCost[2]);

	cout << "Ultimate Description: ";
	getline(cin, Skill[3]);

	cout << "Level 1: ";
	getline(cin, SK4[0]);

	cout << "Level 2: ";
	getline(cin, SK4[1]);

	cout << "Level 3: ";
	getline(cin, SK4[2]);

	cout << "Level 4: ";
	getline(cin, SK4[3]);

	cout << "Level 5: ";
	getline(cin, SK4[4]);

	cout << "Range: ";
	getline(cin, Range[3]);

	cout << "Duration: ";
	getline(cin, Duration[3]);

	cout << "Cooldown: ";
	getline(cin, Cooldown[3]);

	cout << "Mana Cost: ";
	getline(cin, ManaCost[3]);
	
	rpgio << "INTRODUCTION\n";
	rpgio << Name << ", " << Class;
	rpgio << "Strength: " << Strength;
	rpgio << "Agility: " << Agility;
	rpgio << "Intelligence: " << Intelligence;
	rpgio << "Learns Skills: " << Skill[0];
	rpgio << "Attack Range of: " << AttackRange;
	rpgio << "Movement Speed of: " << Movespeed;
	rpgio << "HERO INFORMATION";
	rpgio << "Affiliation: " << Affiliation;
	rpgio << "Role: " << Role;
	rpgio << "HERO ABILTIES";
	rpgio << "Skill 1";
	rpgio << Skill[1];
	rpgio << "Level 1 - " << SK1[0] ;
	rpgio << "Level 2 - " << SK1[1] ;
	rpgio << "Level 3 - " << SK1[2] ;
	rpgio << "Level 4 - " << SK1[3];
	rpgio << "Level 5 - " << SK1[4];
    rpgio << "Range: " << Range[0];
    rpgio << "Duration: " << Duration[0] ;
    rpgio << "Cooldown: " << Cooldown[0];
    rpgio << "Mana Cost: " << ManaCost[0];
	rpgio << "Skill 2";
	rpgio << Skill [2] ;
	rpgio << "Level 1 - " << SK2[0] ;
	rpgio << "Level 2 - " << SK2[1] ;
	rpgio << "Level 3 - " << SK2[2] ;
	rpgio << "Level 4 - " << SK2[3];
	rpgio << "Level 5 - " << SK2[4] ;
    rpgio << "Range: " << Range[1] ;
    rpgio << "Duration: " << Duration[1];
    rpgio << "Cooldown: " << Cooldown[1];
    rpgio << "Mana Cost: " << ManaCost[1];
	rpgio << "Skill 2" ;
	rpgio << Skill [3];
	rpgio << "Level 1 - " << SK3[0];
	rpgio << "Level 2 - " << SK3[1];
	rpgio << "Level 3 - " << SK3[2] ;
	rpgio << "Level 4 - " << SK3[3];
	rpgio << "Level 5 - " << SK3[4] ;
    rpgio << "Range: " << Range[2];
    rpgio << "Duration: " << Duration[2];
    rpgio << "Cooldown: " << Cooldown[2] ;
    rpgio << "Mana Cost: " << ManaCost[2];
	rpgio << "Ultimate" ;
	rpgio << Skill [4] ;
	rpgio << "Level 1 - " << SK4[0];
	rpgio << "Level 2 - " << SK4[1] ;
	rpgio << "Level 3 - " << SK4[2] ;
	rpgio << "Level 4 - " << SK4[3] ;
	rpgio << "Level 5 - " << SK4[4];
    rpgio << "Range: " << Range[3] ;
    rpgio << "Duration: " << Duration[3];
    rpgio << "Cooldown: " << Cooldown[3] ;
    rpgio << "Mana Cost: " << ManaCost[3] ;

	return 0;
}
You're opening "sometext.txt" in rpgio's constructor, and "log.txt" with io::open... Why would you do that? Just say io rpgio("log.txt") Opening it twice probably has some strange side affects.
I tried it with log.txt and it had the same problem so i tried naming it something different.

UPDATE: nah it doesnt work with log.txt as the io rpgio.
Last edited on
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>

using namespace std;

class io{
	private:
			fstream file;
	public:
			io(const char* fname){
				open(fname);}
			~io(){
				if(file.is_open()){
					file.close();}}
			void open(const char* fname){
				file.open(fname,fstream::app);}
			void close(){
				file.close();}
			template<typename T>
			io& operator<<(T& value){
				if(file.is_open()){
					file<<value;}
				cout<<value;
				return *this;}
			template<typename T>
			io& operator>>(T& value){
				cin>>value;
				return *this;}};


int main()
{
	io rpgio("");
	rpgio.open("log.txt");

	string Name;
	string Class;
	string Strength;
	string Agility;
	string Intelligence;
	string Skill[5];
	string AttackRange;
	string Movespeed;
	string Affiliation;
	string Role;
	string SK1[5];
	string SK2[5];
	string SK3[5];
	string SK4[5];
	string Range[4];
	string Duration[4];
	string Cooldown[4];
	string ManaCost[4];

	cout << "Summon name?: ";
	getline(cin, Name);

	cout << "Class?: ";
	getline(cin, Class);

	cout << "Strength?:  ";
	getline(cin, Strength);

	cout << "Agility?: ";
	getline(cin, Agility);

	cout << "Intelligence?: ";
	getline(cin, Intelligence);

	cout << "Learns Skills?: ";
    getline(cin, Skill[0]);

	cout << "Attack Range?: ";
	getline(cin, AttackRange);

	cout << "Movement Speed?: ";
	getline(cin, Movespeed);

	cout << "What is his Affiliation?: ";
	getline(cin, Affiliation);

	cout << "What is his role?: ";
	getline(cin, Role);

	cout << "Skill 1 Description: ";
	getline(cin, Skill[1]);

	cout << "Level 1: ";
	getline(cin, SK1[0]);

	cout << "Level 2: ";
	getline(cin, SK1[1]);

	cout << "Level 3: ";
	getline(cin, SK1[2]);

	cout << "Level 4: ";
	getline(cin, SK1[3]);

	cout << "Level 5: ";
	getline(cin, SK1[4]);

	cout << "Range: ";
	getline(cin, Range[0]);

	cout << "Duration: ";
	getline(cin, Duration[0]);

	cout << "Cooldown: ";
	getline(cin, Cooldown[0]);

	cout << "Mana Cost: ";
	getline(cin, ManaCost[0]);

	cout << "Skill 2 Description: ";
	getline(cin, Skill[2]);

	cout << "Level 1: ";
	getline(cin, SK2[0]);

	cout << "Level 2: ";
	getline(cin, SK2[1]);

	cout << "Level 3: ";
	getline(cin, SK2[2]);

	cout << "Level 4: ";
	getline(cin, SK2[3]);

	cout << "Level 5: ";
	getline(cin, SK2[4]);

	cout << "Range: ";
	getline(cin, Range[1]);

	cout << "Duration: ";
	getline(cin, Duration[1]);

	cout << "Cooldown: ";
	getline(cin, Cooldown[1]);

	cout << "Mana Cost: ";
	getline(cin, ManaCost[1]);

	cout << "Skill 3 Description: ";
	getline(cin, Skill[2]);

	cout << "Level 1: ";
	getline(cin, SK3[0]);

	cout << "Level 2: ";
	getline(cin, SK3[1]);

	cout << "Level 3: ";
	getline(cin, SK3[2]);

	cout << "Level 4: ";
	getline(cin, SK3[3]);

	cout << "Level 5: ";
	getline(cin, SK3[4]);

	cout << "Range: ";
	getline(cin, Range[2]);

	cout << "Duration: ";
	getline(cin, Duration[2]);

	cout << "Cooldown: ";
	getline(cin, Cooldown[2]);

	cout << "Mana Cost: ";
	getline(cin, ManaCost[2]);

	cout << "Ultimate Description: ";
	getline(cin, Skill[3]);

	cout << "Level 1: ";
	getline(cin, SK4[0]);

	cout << "Level 2: ";
	getline(cin, SK4[1]);

	cout << "Level 3: ";
	getline(cin, SK4[2]);

	cout << "Level 4: ";
	getline(cin, SK4[3]);

	cout << "Level 5: ";
	getline(cin, SK4[4]);

	cout << "Range: ";
	getline(cin, Range[3]);

	cout << "Duration: ";
	getline(cin, Duration[3]);

	cout << "Cooldown: ";
	getline(cin, Cooldown[3]);

	cout << "Mana Cost: ";
	getline(cin, ManaCost[3]);
	
	rpgio << "INTRODUCTION\n";
	rpgio << Name << ", " << Class;
	rpgio << "Strength: " << Strength;
	rpgio << "Agility: " << Agility;
	rpgio << "Intelligence: " << Intelligence;
	rpgio << "Learns Skills: " << Skill[0];
	rpgio << "Attack Range of: " << AttackRange;
	rpgio << "Movement Speed of: " << Movespeed;
	rpgio << "HERO INFORMATION";
	rpgio << "Affiliation: " << Affiliation;
	rpgio << "Role: " << Role;
	rpgio << "HERO ABILTIES";
	rpgio << "Skill 1";
	rpgio << Skill[1];
	rpgio << "Level 1 - " << SK1[0] ;
	rpgio << "Level 2 - " << SK1[1] ;
	rpgio << "Level 3 - " << SK1[2] ;
	rpgio << "Level 4 - " << SK1[3];
	rpgio << "Level 5 - " << SK1[4];
    rpgio << "Range: " << Range[0];
    rpgio << "Duration: " << Duration[0] ;
    rpgio << "Cooldown: " << Cooldown[0];
    rpgio << "Mana Cost: " << ManaCost[0];
	rpgio << "Skill 2";
	rpgio << Skill [2] ;
	rpgio << "Level 1 - " << SK2[0] ;
	rpgio << "Level 2 - " << SK2[1] ;
	rpgio << "Level 3 - " << SK2[2] ;
	rpgio << "Level 4 - " << SK2[3];
	rpgio << "Level 5 - " << SK2[4] ;
    rpgio << "Range: " << Range[1] ;
    rpgio << "Duration: " << Duration[1];
    rpgio << "Cooldown: " << Cooldown[1];
    rpgio << "Mana Cost: " << ManaCost[1];
	rpgio << "Skill 2" ;
	rpgio << Skill [3];
	rpgio << "Level 1 - " << SK3[0];
	rpgio << "Level 2 - " << SK3[1];
	rpgio << "Level 3 - " << SK3[2] ;
	rpgio << "Level 4 - " << SK3[3];
	rpgio << "Level 5 - " << SK3[4] ;
    rpgio << "Range: " << Range[2];
    rpgio << "Duration: " << Duration[2];
    rpgio << "Cooldown: " << Cooldown[2] ;
    rpgio << "Mana Cost: " << ManaCost[2];
	rpgio << "Ultimate" ;
	rpgio << Skill [4] ;
	rpgio << "Level 1 - " << SK4[0];
	rpgio << "Level 2 - " << SK4[1] ;
	rpgio << "Level 3 - " << SK4[2] ;
	rpgio << "Level 4 - " << SK4[3] ;
	rpgio << "Level 5 - " << SK4[4];
    rpgio << "Range: " << Range[3] ;
    rpgio << "Duration: " << Duration[3];
    rpgio << "Cooldown: " << Cooldown[3] ;
    rpgio << "Mana Cost: " << ManaCost[3] ;

	return 0;
}


Using ("") instead of ("log.txt") worked...dont know why, but thanks for all the help and im sorry for being so bad at this =(
Ok... What I mean is this:

1
2
3
4
5
6
7
int main(){
	io rpgio("log.txt");
	
	string Name;
	//... Put the rest here
	}
Topic archived. No new replies allowed.