Reading Data from Text File Problem?

I'm writing a code for password keeping. Unfortunately, in the main() I can not pull "out password" from the code. Whenever I run it on my computer, the test for the outpassword is always blank showing me that there is nothing saved in that variable. I am very confused and I have read the tutorial for the files several times and followed what it said, for some reason my program is not working?...

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int passwordmethod(string filename)
{
    
    string outline1;
    string outline2;
    string outline3;
    string outline4;
    string outline5;
    string outline6;
    string outline7;
    
    ifstream infile;    
    string input;
    string inputoutput;
    
    cout << endl;
    cout << "INPUT Passwords or OUTPUT: ";
    cin >> inputoutput;
    
    if(inputoutput=="input"){
    
    cout << endl;
    cout << "Please enter the DESCRIPTION of your PASSWORD,";
    cout << endl;
    cout << "then click ENTER and then the password itself.";
    cout << endl;
    cout << "Once you've finished entering all your passwords,";
    cout << endl;
    cout << "Hit '*' and then ENTER.";
    cout << endl;
    cout << endl;
    
    getline(cin, input, '*');

    infile.open(filename.c_str());

    infile >> input;
    
    infile.close();
    
    }
    
    else if(inputoutput=="output"){
        
        ofstream myfile;
        myfile.open(filename.c_str());
        
        myfile << outline1;
        myfile << outline2;
        myfile << outline3;
        myfile << outline4;
        myfile << outline5;
        myfile << outline6;
        myfile << outline7;
        
        cout << outline2 << endl;
        cout << outline3 << endl;
        cout << outline4 << endl;
        cout << outline5 << endl;
        cout << outline6 << endl;
        cout << outline7 << endl;
        
    }
    
    else{
        cout << "ERROR INCORRECT INPUT!";
        cout << endl;
        cout << "Restarting...";
        cout << endl;
        int main();
    }
    
    return 0;
    
}

int main()
{
    
    ofstream myfile;
    
    cout << endl;
    cout << "------------------";
    cout << endl;
    cout << "PASSWORD PROTECTOR";
    cout << endl;
    cout << "------------------";
    cout << endl;
    cout << endl;
    
    string repeat;
    string filename;
    string username;
    string inusername;
    string outusername;
    string password;
    string inpassword;
    string outpassword;
    string choice;
    
    cout << "REGISTER or LOGIN: ";
    cin >> choice;
    cout << endl;
    
    if(choice=="login"){
    
    cout << "USERNAME: ";
    cin >> username;
    cout << endl;
    cout << "PASSWORD: ";
    cin >> password;
    cout << endl;
        
    filename = username;
    
    ifstream infile; 
    infile.open(filename.c_str()); // I opened the file here, but for some reason it's not getting the outpassword...
        
    infile >> outpassword; //<<<This is the line that's confusing me!!!

	cout << "Test Username: " + username << endl;
	cout << "Test Password: " + password << endl;
	cout << "Test outpassword: " + outpassword << endl; // And then this line always is blank so I think it's not getting an outpassword at all…?

        if(outpassword==password){
            cout << endl;
            cout << "SUCCESSFUL LOGIN!";
            cout << endl;
            passwordmethod(filename);
        }
        
        else{
            cout << endl;
            cout << "INCORRECT LOGIN!";
            cout << endl;
            cout << "ABORTING PROGRAM...";
            cout << endl;
	cout << endl;
        }
        infile.close();
    }
    
    else if(choice=="register"){
        
        cout << "USERNAME: ";
        cin >> inusername;
        cout << endl;
        cout << "PASSWORD: ";
        cin >> inpassword;
        cout << endl;
        
        filename = inusername;
                
        myfile.open(filename.c_str());
        myfile << inpassword;
        myfile << endl;
        
        cout << endl;
        cout << "Account successfully created.";
        cout << endl;
        
        myfile.close();
    }
    
    else{
        cout << "ERROR INCORRECT INPUT!";
        cout << endl;
        cout << "RESTARTING...";
        cout << endl;
        int main();
    }
    
}
Last edited on
closed account (o3hC5Di1)
Hi there,

You may want to check whether the file was actually opened successfully or not, as a debug statement, for example:

1
2
3
4
5
6
7
ifstream infile; 
    infile.open(filename.c_str()); // I opened the file here, but for some reason it's not getting the outpassword...
 
    if(!infile.is_open())
        std::cout << "File " << filename << " could not be opened.";
        
    infile >> outpassword; //<<<This is the line that's confusing me!!! 


That will show you if the program was unable to open the file.
If not, it's worth trying the register part of your program , checking whether it actually creates a file or not.

Hope that helps you on your way, please do let us know if you require any further help.

All the best,
NwN
Thanks NwN that helped me solve it! :D
Last edited on
Topic archived. No new replies allowed.