Program Won't save to right directory.

I've messed and messed with this block of code but a small and important part won't work. It's the password thing again (I know I've asked questions about it before), but when I tell the computer to make the file it won't here's the whole thing.



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
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <windows.h>
#include <cstdlib>
#include <time.h>


using namespace std;


int staticLoad() // Exterior function
{
	cout << "Loading";
	Sleep(100); // A more simple delay that works by milliseconds. It goes along with the Windows header
	cout << ".";
	Sleep(100);
	cout << ".";
	Sleep(100);
	cout << ".";
	Sleep(800);
    system("cls");
	cout << "Loading";
	Sleep(100);
	cout << ".";
	Sleep(100);
	cout << ".";
	Sleep(100);
	cout << "." << flush; // Flush just clears the input buffer. Its not needed but is a good thing to use every now and then.
   system("cls");
	return 0;
}
int WriteToText()
{
    ///////  Variables
 const   int SIZE =100;
    string szNewUsername; // for a new username
    string szNewPassword;// for a new password


system("cls");
        cin.clear();
        ofstream WriteFile; // write to a file
        cout << "Type in your username or type in a new one.\n                                      -";
        cin  >> szNewUsername;
         system("cls");
        cout << "Type in your new password.\n                  -";
        cin  >> szNewPassword;
        string WritePath;
        WritePath = "C:\\Usernames\\";
        string szNewTogether;
        szNewTogether == WritePath + szNewUsername;
cout << szNewTogether;
system("pause>nul");
 WriteFile.open(szNewTogether); // makes new file //  szNewTogether.c_str() <-Makes string act like char

if (WriteFile.fail())
{
system("cls");
cout << "error";
}




    WriteFile << szNewPassword;// saves password to file
    WriteFile.close();



return 0;

}







int main(int nNumberofArgs, char* pszArgs[])
 {
     system("cls");
     system("title Grace Assembly of God - Login");
     keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0); //not windows 7 for some reason... Not sure if it will work because of my limitations of OS.
    keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0);
    keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), KEYEVENTF_KEYUP, 0);
    keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);

staticLoad();
   system("color a"); // Simple system color [green]
	time_t rawtime;    // time stamp stuff so that the time the program is executed is logged in an external text file.
	time ( &rawtime );
string szTime = ctime (&rawtime); // Set the timestamp equal to a alpha variable.

    string szUsername;
    string line;
    string szPassword;


    cout << "Type in your username.\n                                -";
    cin  >> szUsername;

    if (szUsername == "new" || szUsername == "New" || szUsername == "update" || szUsername == "Update")
    {



WriteToText();// previous function

return 0;

  }


ofstream ofLogFile;
ofLogFile.open("C:\\Password Log\\log.txt",ios::app);
ofLogFile << "\n[ACCESS ENTRY]\nDATE: " << szTime << "\nUSERNAME: " << szUsername;
ofLogFile.close();

 if (szUsername != "new" || szUsername != "New" || szUsername != "update" || szUsername != "update")// Does not equal
 {

string szExistingTogether;
szExistingTogether == "C:\\Usernames\\" + szUsername;
cout << szExistingTogether;
  ifstream ReadFile;
  ReadFile.open(szExistingTogether.c_str());
  if ( ReadFile.good() ) // if it finds it
    {
        system("cls");
      getline (ReadFile,line);
      cout << "Type your password.\n                   -";
      cin  >> szPassword;
      ofLogFile << "\nPASSWORD: " << szPassword << "\n\n\n";

      if(szPassword == line)
      {
          system("cls");
          cout << "Correct";
          Sleep(200);
          return 0;

    };
    if (szPassword != line)
    {
        system("cls");
        cout << "Wrong password, logging off in 60 seconds";
        ofLogFile << "^^^^^^^^^Password wrong\n\n\n";
        system("shutdown /f /t 60");
        Sleep(2000);
    ReadFile.close();



};
    };
if (ReadFile.fail())// if it can't find it
{
    system("cls");




    cout << "Cannnot be opened";
    Sleep(2000);
    ofLogFile << "^^^^^^^^^Username wrong, no password entry\n\n\n";
    system("shutdown /f  /t 60");
    system("pause>nul");
    return 0;
}




}
 ofLogFile.close();
return 0;
 }



It's the function WriteToText that won't work. It just doesn't make the file in the designated location. Now if I take out the location for it to make the file in, it just makes it in the local 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
int WriteToText()
{
    ///////  Variables
 const   int SIZE =100;
    string szNewUsername; // for a new username
    string szNewPassword;// for a new password


system("cls");
        cin.clear();
        ofstream WriteFile; // write to a file
        cout << "Type in your username or type in a new one.\n                                      -";
        cin  >> szNewUsername;
         system("cls");
        cout << "Type in your new password.\n                  -";
        cin  >> szNewPassword;
        string WritePath;
        WritePath = "C:\\Usernames\\";
        string szNewTogether;
        szNewTogether == WritePath + szNewUsername;
cout << szNewTogether;
system("pause>nul");
 WriteFile.open(szNewTogether); // makes new file  // szNewTogether.c_str() <-Makes string act like char
Last edited on
szNewTogether.c_str() is what I had in there. I took it out to see if that would work too but it didn't and I forgot to put it back in there.
This line is wrong in this context:

1
2
szNewTogether == WritePath +
szNewUsername;
How's it wrong? I've tried the single equal sign, and the double eaqual sign.
Single Equal "=" - Set the variable(s) on the left to the variable on the right.

Double Equal "==" - Check to see if the variable(s) on the left and right are equal.

Also, your spacing is a little inconsistant which is a little annoying. But if you are the person I'm remembering then you've gotten much better.
Line 127 in the first snippet is wrong (the "==" sign)

and

Line 20 in the second snippet is also wrong (the "==" sign)

Without going through the rest of your code, those two errors alone could throw off the whole thing.
Thanks. @ComputerGeek01, it happened when I copied the source to here. I've fixed the two problems, but it won't save to the right directory which is weird. It shows the right location, but doesn't write to the correct location.


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
int WriteToText()
{
    ///////  Variables
 const   int SIZE =100;
    string szNewUsername; // for a new username
    string szNewPassword;// for a new password


system("cls");
        cin.clear();
         // write to a file
        cout << "Type in your username or type in a new one.\n                                      -";
        cin  >> szNewUsername;
         system("cls");
        cout << "Type in your new password.\n                  -";
        cin  >> szNewPassword;



        string szWritePath;
        szWritePath = "C:\\Program Files\\Usernames\\" + szNewUsername;


cout << szWritePath;
system("pause>nul");

  ofstream WriteFile;
 WriteFile.open(szWritePath.c_str()); // makes new file // Need to add the full path // szNewTogether.c_str() <-Makes string act like char




    WriteFile << szNewPassword;// saves password to file

if (WriteFile.fail())
{
system("cls");
cout << "error";
};



WriteFile.close();

return 0;

}
Last edited on
It's because I don't have the right permissions.
Topic archived. No new replies allowed.