Guest User

Untitled

a guest
Jan 18th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. string path = "account.txt"; //path to the text file that we will search
  9.  
  10. ifstream in; //instance to read files
  11. in.open(path); //open the file we want to search
  12.  
  13. //check if the file is not open or faield to open
  14. if (!in.is_open() || in.fail()) {
  15. cerr << "Error: Cannot access file!" << endl;
  16. }
  17.  
  18.  
  19.  
  20. bool found = false; //check to see if we found what we're looking for
  21. string result; //the results found in the txt file
  22. int line = 0; //the current line the application is searching
  23.  
  24. bool correct = false; //check if the password is correct
  25.  
  26. while (!in.eof()) {
  27. getline(in, result); //adds results to a string for the application to read
  28.  
  29. line ++; //increment everytime we loop to new line
  30.  
  31. //finds the index of where the word starts
  32. size_t index = result.find("connelly");
  33.  
  34. if (result.find("password=")) {
  35. cout << "Checking password on line: " << (line + 1) << endl;
  36. found = true;
  37. }
  38.  
  39. //check password if found password field
  40. if (found) {
  41. if (index!=string::npos) {
  42. correct = true;
  43. cout << "Index: " << (index + 1) << " line: " << line << endl;
  44. } else if (correct = false) {
  45. cout << "Invalid password!" << endl;
  46. } else {
  47. cerr << "Error: Could not fetch password!" << endl;
  48. }
  49. break; //exit the loop
  50. }
  51.  
  52. }
  53.  
  54. in.close(); //close the file after we're done with it
  55.  
  56. system("pause"); //pause the console so the application does not close right away
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment