Quick Question about <fstream>

Is there a way to make it to where it does not overwrite the file everytime you run the program? I have this so far but it overwrites what I wrote to the document the last time.
Thanks garrows

1
2
3
ofstream outputFile;
outputFile.open("Password.txt");
outputFile << "Your password is:";
Make sure to read the descriptions of the functions you call, it's a huge help

The description of the function you're calling here can be found in both C++ references:
http://www.cplusplus.com/reference/fstream/basic_ofstream/open/
http://en.cppreference.com/w/cpp/io/basic_ofstream/open

It sounds like you're looking for the app (append) open mode

also note that using two-phase init is a bad habit;
1
2
    std::ofstream outputFile("Password.txt", std::ios::app);
    outputFile << "Your password is:\n";

demo: http://coliru.stacked-crooked.com/a/08aa63892373c45a
Chubbi Sorry for the bad habbit, I didn't quite understand what you said, Ill post my code in here

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <Windows.h>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <limits>
#include <stdexcept>
#include <string.h>
#include <cctype>
using namespace std;


static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int size = sizeof(alphanum) - 1;

int main()

{
int length;
ofstream outputFile("Password.txt", ios::app);
outputFile.open("Password.txt");
if (outputFile.is_open())
{
    outputFile << "File successfully opened" <<endl;
}
else
{
    cout << "Error opening file" <<endl;
}
outputFile << "Your password is:";

	//password length
	cout << "Welcome to garrow's password generator" <<endl;
	Sleep(1500);
	cout << "...............Please Hold..............." <<endl;
	Sleep(5000);
	system("cls");
	cout << "Please enter the amount of characters you want your password to be."<<endl;
	cin >> length;
	char a;
	cout << "Are you sure?(Y/N)" <<endl;
	cin >> a;
	Sleep(250);
	system("cls");
    switch (toupper( a ))
    {
        case 'Y':{ cout << "Your password is" <<endl;
	//-----------------------password generation-------------------
	char pwd[1000] = {0};
    srand(time(0));
    for (int password = 0; password < length; password++)
   {
    pwd[password] = alphanum[rand() % size];
    cout << pwd[password];
}
	outputFile << " " << pwd << " " <<endl;
	outputFile.close();
	Sleep(99999999999999);
	return 0;}
	break;
        case 'N':{

       cout << "Please enter the correct amount of characters you want your password to be." <<endl;
       cin >> length;
       cout << "Your password is" <<endl;
	//-----------------------password generation-------------------
	char pwd[1000] = {0};
    srand(time(0));
    for (int password = 0; password < length; password++)
   {
    pwd[password] = alphanum[rand() % size];
    cout << pwd[password];
}
	outputFile << " " << pwd << " " <<endl;
	outputFile.close();
	Sleep(99999999999999);
	return 0;

        }
    }
}

What im trying to do is have the code not create a new document everytime but instead create a new line in the document then write the 2nd run password or 3rd run password.

-Garrows
After fixing line 27 (which opens the file in append mode), you forgot to remove line 28 (which opens the file in replace mode)
Topic archived. No new replies allowed.