files and passwords

Hi I am trying to make a program that asks for password when you try to open a file. Any Ideas?

Thanks in advance!


I tried with that, obviously without success :D
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
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>

using namespace std;

int main ()
{

    ifstream file("test.txt");
    string password;
    file.close();
    while(true)
    {
        if (file.is_open())
        {
            file.close();
            cout<<"Enter password to use file: ";
            cin>>password;
            if(password == "hello")
            {
                cout<<"success\n";
                system("start test.txt");
                Sleep(10000);
            }
            if(password != "hello")
            {
                cout<<"wrong password\n";
                file.close();
            }
        }
    }
    system("pause");
    return 0;
}
If I understood it correctly, you must create a system "dogwatch" (If I'm right about your case, the Windows Handle API for files).
BTW, I have already thought in implementing that... Sometimes you just really don't need it.
Good luck!
hey lets look upon it Nanyo :

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

using namespace std;

int main ()
{

    ifstream file("test.txt");
    string password;
    file.close();
   // while(true)
   // {
       // if (file.is_open())
       // {
            file.close();
            cout<<"Enter password to use file: ";
            cin>>password;
            if(password == "hello")
            {
                cout<<"success\n";
                system("start test.txt");
                Sleep(10000);
            }
            if(password != "hello")
            {
                cout<<"wrong password\n";
                file.close();
            }
     //   }
    //}
    system("pause");
    return 0;
}
Last edited on
Here is a small variation of what you're trying:

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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
  std::string fileName;
  std::string filePassword;
  
  std::cout << "Please enter the name of the file you'd like to open: ";
  std::cin >> fileName;

  std::ifstream myFile(fileName);

  if(!myFile.is_open())
    {
      std::cout << "File does not exist" << std::endl;
      exit(EXIT_FAILURE);
    }

  std::cout << "Please enter the password to read this file: ";
  std::cin >> filePassword;

  if(filePassword == "hello")
    {
      while(!myFile.eof())
        {
          std::cout << char(myFile.get());
        }
    }
  else
    {
      std::cout << "Incorrect Password" << std::endl;
    }

  myFile.close();

  return 0;
}


This small program first asks for the file in which to open. If the file doesn't exist, it tells you so, and exits (It looks, by default, in the directory the program is being run from). If the file does exist, the program continues to ask for a password, if the password typed by the user is "hello", I output the file to the user by iterating through the file character by character (checking that the current character isn't the EOF [End of File] and output that character). Otherwise, I tell them the password is incorrect. I finish by closing the file. Make sense?
Last edited on
@OP Explain it better, please. What do you want? The users input a file to be open and asks for a password (INSIDE THE PROGRAM)? Or do you want that, in desktop, when user tries to open the file, a pop-up appears and asks for a password?
iQChange

if i want that in desktop, when user tries to open the file, a pop-up appears and asks for a password? then what the code is likely to be? can you explain? and another thing if there is some way can it be applied on folders also? If yes please mention the code.
Here is another version hope it helps

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int main()
{
fstream in;
int i = 3;
char psword[60];
char filename[20];

cout << "You have only 3 attempt!" << endl;
for(int j = 0; j < i; j++) {
cout << "Please enter password to access file: "<< endl;
cin >> psword;

if(strcmp(psword, "zamuxolo") == 0) {
cout << "Please enter file name:" << endl;
cin >> filename;

in.open(filename);

if(!in) {
cout << filename << " not found!" << endl;
}
else {
cout << filename << " opened." << endl;
}
}
else {
cout << "Wrong password." << endl;
}
in.close();
}
return 0;
}
Last edited 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
#include <iostream>
 #include <fstream>
 #include <string.h>

 using namespace std;

 int main()
 {
 fstream in;
 int i = 3;
 char psword[60];
 char filename[20];

 cout << "You have only 3 attempt!" << endl;
 for(int j = 0; j < i; j++) {
 cout << "Please enter password to access file: "<< endl;
 cin >> psword;

 if(strcmp(psword, "zamuxolo") == 0) {
 cout << "Please enter file name:" << endl;
 cin >> filename;

 in.open(filename);

 if(!in) {
 cout << filename << " not found!" << endl;
 }
 else {
 cout << filename << " opened." << endl;
 }
 }
 else {
 cout << "Wrong password." << endl;
 }
 in.close();
 }
 return 0;
 }
Topic archived. No new replies allowed.