How do i create multiple files in/from ofstream?

Hello everyone,I have a question on file manipulation and would like to know how
could i create multiple files from ofstream. For example in the code below when option 4 is selected a file is created called "Progress.txt" but i want to know how to enable the program to create a new file like "Progress1.txt" instead of just overwriting the old existing one,kinda like a database but i do not want to rewrite the information in the old file instead i want a new file created with the new information stored in it (Keeping records).I do not know how to do that in code form.

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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
//variables
char Devotion = '\0';
int Devpoints = 0;
int Pushups = 0;
int Pushpoint=0;
int Situps = 0;
int Sitpoint = 0;
int Gym = 0;
int Gympoints = 0;
char Research = '\0';
int Researchpt = 0;
int Texts =0;
int Textspt = 0;
int Words = 0;
int Wordspt = 0;
int Puzzles = 0;
int Puzzlespt =0;
int Unblockme = 0;
int Unblockmept = 0;
int Sudoku = 0;
int Sudokupt = 0;
int Somethingnew = 0;
int Somethingnewpt=0;
int Bed = 0;
int Bedpt = 0;
int Book = 0;
int Bookpt = 0;
int RBook=0;
int Total=0;
char c='\0';
int Option=0;
string Name = "";
//string Last[14] = {};
//int Lresults[14] = {};
//int i = 0;

//Getting the user informatiions
     cout<<"Please enter your name:"<<endl;
     getline(cin,Name);
     system("cls");




do {

    cout<<"WELCOME "<<Name<<endl<<endl;
    cout<<"Please Choose an option from the menu below"<<endl
         <<"1. ENTER YOUR PROGRESS FOR THE WEEK."<<endl
         <<"2. VIEW YOUR TOTAL SCORE FOR THE CURRENT WEEK."<<endl
         <<"3. TO CLEAR THE SCREEN."<<endl
         <<"4. TO SAVE YOUR CURRENT PROGRESS IN A FILE."<<endl
         //<<"5. TO VIEW YOUR PROGRESS FROM LAST WEEK."<<endl
         <<"Please choose and option:"<<endl;
         cin>>Option;





    switch (Option){
    case 1:

  cout<<"1) Have you done all your devotions for the week? y or n"<<endl;
  cin>>Devotion;

  cout<<"2) How many Pushups have you done for the week"<<endl;
  cin>>Pushups;

  cout<<"3) How many Situps have you done for the week"<<endl;
  cin>>Situps;

  cout<<"4) How many Gym days have you gone for the week"<<endl;
  cin>>Gym;

  cout<<"5) Did you do your research for the week? y or n"<<endl;
  cin>>Research;

  cout<<"6) How many Texts have you learnt for the week?"<<endl;
  cin>>Texts;

  cout<<"7) How many words have you learnt for the week?"<<endl;
  cin>>Words;

  cout<<"8) How many Puzzles have you done for the week?"<<endl;
  cin>>Puzzles;

  cout<<"9) How many unblockme's have you done for the week?"<<endl;
  cin>>Unblockme;

  cout<<"10) How many Sudoku Puzzles have you done for the week?"<<endl;
  cin>>Sudoku;

  cout<<"11) How many things did you learn this week?"<<endl;
  cin>>Somethingnew;

  cout<<"12) How many times did you make you Bed for this week?"<<endl;
  cin>>Bed;

  cout<<"13 )Did you read a book?\n(0)Did not read\n(1)Started\n(2)enter the amount of books you have read"<<endl;
  cin>>Book;

  if (Book==2){cout<<"14) How many Books have you read for this week?"<<endl;cin>>RBook;}

     //calculatons
//calculations for devotions
      if (Devotion=='y') {
                            Devpoints=6;
                         }
      else if (Devotion=='n'){
                               Devpoints=3;
                            }


//calculations for push ups
   if (Pushups==450){
                      Pushpoint=3;
                    }
    else if (Pushups>450){
                           Pushpoint=((Pushups-450)/50)+3;
                         }
//calculatons for situps

if (Situps==500){
                      Sitpoint=3;
                    }
    else if (Pushups>500){
                           Sitpoint=((Situps-500)/50)+3;
                         }

 //calculations for Gym
 if (Gym==0){
             Gympoints=0;
             }
else if (Gym>0){
                Gympoints=Gym*5;
                  }

//Calculations for Research
if (Research=='y')
     {
         Researchpt = 5;
     }
  else if (Research=='n')
  {
      Researchpt=0;
  }

//Calculatuons for bible texts
   if (Texts==0)
   {
      Textspt=0;
   }
   else if (Texts>0)
   {
       Textspt=Texts*2;
   }

   //calculatons for words
     if (Words==0)
   {
      Wordspt=0;
   }
   else if (Words>0)
   {
       Wordspt=Words*2;
   }


//calculatons for Puzzles
     if (Puzzles==0)
   {
      Puzzlespt=0;
   }
   else if (Puzzles>0)
   {
       Puzzlespt=Puzzles*2;
   }

   //calculatons for Unblockme
     if (Unblockme==0)
   {
      Unblockmept=0;
   }
   else if (Unblockme>0)
   {
       Unblockmept=Unblockme*2;
   }

   //calculatons for Sudoku
     if (Sudoku==0)
   {
      Sudokupt=0;
   }
   else if (Sudoku>0)
   {
       Sudokupt=Sudoku*2;
   }

 //calculatons for Something new
     if (Somethingnew==0)
   {
      Somethingnewpt=0;
   }
   else if (Somethingnew>0)
   {
       Somethingnewpt=Somethingnew*2;
   }

  //calculations for making bed
if (Bed==0)
   {
    Bedpt=0;
   }
   else if (Bed>0)
   {
       Bedpt=Bed*2;
   }

  //Calculautons for reading books
    if (Book==0)
    {
        Bookpt=0;
    }
else if (Book==1)
{
    Bookpt=2;
}
else if (RBook>0)
{
  Bookpt=RBook*5;
}

    Total=Devpoints+Pushpoint+Sitpoint+Gympoints+Researchpt+Textspt+Wordspt+Puzzlespt+Unblockmept+Sudokupt+Somethingnewpt+Bedpt+Bookpt;
break;


 case 2 :

  //output of the program
  cout<<"  YOUR SCORE FOR THIS WEEK :) "<<Name<<endl;
   cout<<"Devotion:      "  <<Devpoints<<" Pts"<<endl;
   cout<<"Push-ups:      "  <<Pushpoint<<" Pts"<<endl;
   cout<<"Sit-ups:       "   <<Sitpoint<<" Pts"<<endl;
   cout<<"Gym:           "   <<Gympoints<<" Pts"<<endl;
   cout<<"Research:      "   <<Researchpt<<" Pts"<<endl;
   cout<<"Texts:         "   <<Textspt<<" Pts"<<endl;
   cout<<"Words:         "   <<Wordspt<<" Pts"<<endl;
   cout<<"Puzzles:       "   <<Puzzlespt<<" Pts"<<endl;
   cout<<"Unblockme:     "    <<Unblockmept<<" Pts"<<endl;
   cout<<"Sudoku:        "    <<Sudokupt<<" Pts"<<endl;
   cout<<"Something New: "    <<Somethingnewpt<<" Pts"<<endl;
   cout<<"Making Bed:    "    <<Bedpt<<" Pts"<<endl;
   cout<<"Books:         "    <<Bookpt<<" Pts"<<endl;
   cout<<"Total:         "    <<Total<<" Pts"<<endl;
break;



case 3:
          system("cls");
          break;




default :
                    cout<<"Thank you...."<<endl;
                    break;

    }

    if (Option==4){

         ofstream Progress;
         Progress.open ("Progress.txt");
   Progress<<"  YOUR SCORE FOR THIS WEEK :) "<<Name<<endl;
   Progress<<"Devotion:      "  <<Devpoints<<" Pts"<<endl;
   Progress<<"Push-ups:      "  <<Pushpoint<<" Pts"<<endl;
   Progress<<"Sit-ups:       "   <<Sitpoint<<" Pts"<<endl;
   Progress<<"Gym:           "   <<Gympoints<<" Pts"<<endl;
   Progress<<"Research:      "   <<Researchpt<<" Pts"<<endl;
   Progress<<"Texts:         "   <<Textspt<<" Pts"<<endl;
   Progress<<"Words:         "   <<Wordspt<<" Pts"<<endl;
   Progress<<"Puzzles:       "   <<Puzzlespt<<" Pts"<<endl;
   Progress<<"Unblockme:     "    <<Unblockmept<<" Pts"<<endl;
   Progress<<"Sudoku:        "    <<Sudokupt<<" Pts"<<endl;
   Progress<<"Something New: "    <<Somethingnewpt<<" Pts"<<endl;
   Progress<<"Making Bed:    "    <<Bedpt<<" Pts"<<endl;
   Progress<<"Books:         "    <<Bookpt<<" Pts"<<endl;
   Progress<<"Total:         "    <<Total<<" Pts"<<endl;
  Progress.close();


break;


    }

 cout<<"\n\nDo you want to continue y or n?:"<<endl;
cin>>c;
system("cls");//clear screen
}
    while ((c=='y')||(c=='Y'));


system("pause");

return 0;
}
Last edited on
http://www.cplusplus.com/reference/fstream/fstream/open/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// fstream::open
#include <fstream>
using namespace std;

int main () {

  fstream filestr;

  filestr.open ("test.txt", fstream::in | fstream::out | fstream::app);

  // >> i/o operations here <<

  filestr.close();

  return 0;
}


If there isn't a file called test.txt, fstream::app creates one.
Last edited on
Thank you for responding . But instead overwriting i want it to create a new file everytime the option is choosen,so first time i run it i creates test.txt but the next time i run it i want it to create a new file called test1.txt and so 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
#include <fstream>
#include <iostream>

#define TRUE true

int main()
{
	char filename[32];
	std::string user_input;
	std::fstream myfile;
	
	while(TRUE)
	{
		std::cout<<"Name of file?  ";
		std::cin>>filename;
		std::cin.ignore();
	
		myfile.open(filename);
	
		if(myfile.is_open())
		{
			std::cout<<"File is already created. Would you like to create a new file (y/n) ?  ";
			std::cin>>user_input;

			if(user_input=="y")
			{
				std::cout<<"Name of new file + copy ending + type?  ";
				std::cin>>filename;
				std::fstream myfile_new;
				myfile_new.open (filename, std::fstream::in | std::fstream::out | std::fstream::app);
				myfile_new.close();
			}
			else
			{
				break;
			}
		}
		else
		{
			std::cout<<"This file does not exist yet. Would you like to create the file (y/n) ?  ";
			std::cin>>user_input;

			if(user_input=="y")
			{
				std::cout<<"Name of new file + copy ending + type?  ";
				std::cin>>filename;
				std::fstream myfile_new;
				myfile_new.open (filename, std::fstream::in | std::fstream::out | std::fstream::app);
				myfile_new.close();
			}
			else
			{
				break;
			}
		}
	}
	myfile.close();
}


Like this?
Topic archived. No new replies allowed.