Writing a file to a specified path...

On line 18, I'd like this file to save in a folder in the same directory called "Usernames". Because I'm using c_str(), it will create a str file instead of a .txt file. How do I save this file to that folder?

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

void addUser()
    {
        string newUser;
        string newPass;
        cout << "Please enter a Username: ";
        cin >> newUser;
        cout << "\nPlease enter a Password: ";
        cin >> newPass;
        string fname = newUser;
        fname += ".txt";
        ofstream myfile;
        myfile.open(fname.c_str());
        myfile << "Username: " << newUser;
        myfile << "\nPassword: " << newPass;
        myfile << "\nPrivelege level: ";
        myfile.close();

    }

int main()
{
    addUser();
    return(0);
}
Also I'm trying to make a method to scan a file for a keyword.
In this file will be a username and password. The line that contains the password precedes the username.

I'm thinking the code will look something like this but I need help.
Will annotate code below...
1
2
3
4
5
6
7
8
9
10
11
getline(myfile, line)
     while(line < myfile.length)
{
start:
if(line.toCharArray[0,7] == "password")
return line.toCharArray[8, line.toCharArray.length] 
else
line = nextLine;
goto start;
}

On line 2, how do I define the length of myfile according to the amount of lines it has?
On line 5, how to I take that line and convert it into a char array to test if it is the line that contains the password?
On line 8, is this the correct way to change the line?

None of this code has been written into an IDE yet because it's just my brainstorming in my notebook. Hoping for some feedback.
Last edited on
The filename that you use to open the ofstream can be an absolute or relative path to the file.
1
2
string fpath = "dir/" + newUser + ".txt";
file.open(fpath.c_str());

Note that ofstream will fail if the directory does not already exist, so you have to make sure it has been created before you try to open the file.
Last edited on
Tenom722 wrote:
On line 2, how do I define the length of myfile according to the amount of lines it has?

You can't know how many lines there are unless you scan the whole file first, but maybe you don't need to know the number of lines. You could use line 1 as a loop condition and it will stop the loop when getline fails to read a line (at the end of the file).
 
while(getline(myfile, line))


Tenom722 wrote:
On line 5, how to I take that line and convert it into a char array to test if it is the line that contains the password?

Why do you need to convert it into an array? Just use the line variable.
 
if(line == "password")


Tenom722 wrote:
On line 8, is this the correct way to change the line?

Not sure what you are trying to do here. Changing the line variable will not change the file content. If you want to read the next line and run the loop again I think you should just remove the else part and the goto statement and let the loop condition read in the next line (see my recommendation for line 2 above).
Last edited on
I need to convert it into an array because I only need part of the line that starts with password: the rest of the line after "password: " that actually contains the password.
UPDATED CODE:

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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
bool UserVerified;
void addUser()
    {
        string newUser;
        string newPass;

            cout << "Please enter a Username: ";
            cin >> newUser;
            cout << "\nPlease enter a Password: ";
            cin >> newPass;
            string fname = "Usernames/" + newUser + ".txt";
            ofstream myfile;
            myfile.open(fname.c_str());
            myfile << "Username: " << newUser;
            myfile << "\nPassword: " << newPass;
            myfile << "\nAdministration: ";
            myfile.close();

    }
    string findPass()
    {

        string UserEntered;
        string PassEntered;
        string line;
        string passwordRetrieved;
        cout << "Please sign in." << "\nUsername: ";
        cin >> UserEntered;
        cout << "\nPassword: ";
        cin >> PassEntered;
        ifstream myfile;
        string fname = "Usernames/" + UserEntered + ".txt";
        myfile.open(fname.c_str());
        while(myfile.good())
        {
            start:
                getline(myfile, line);
                if(line == "Password")
                {
                    passwordRetrieved = line;
                    myfile.close();
                    return passwordRetrieved;
                }
                else
                {
                    goto start;
                }
        }
        if(passwordRetrieved == "Password: " + PassEntered)
        {
            UserVerified = true;

        }
        else
        {
            cout << "Invalid Password";
        }
        return passwordRetrieved;
    }
int main()
{
    findPass();
    if(UserVerified)
    {
        addUser();
    }
    else
    {
        cout << "Insufficient permissions";
        return (0);
    }
}
Last edited on
Topic archived. No new replies allowed.