Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- int main() {
- string path = "account.txt"; //path to the text file that we will search
- ifstream in; //instance to read files
- in.open(path); //open the file we want to search
- //check if the file is not open or faield to open
- if (!in.is_open() || in.fail()) {
- cerr << "Error: Cannot access file!" << endl;
- }
- bool found = false; //check to see if we found what we're looking for
- string result; //the results found in the txt file
- int line = 0; //the current line the application is searching
- bool correct = false; //check if the password is correct
- while (!in.eof()) {
- getline(in, result); //adds results to a string for the application to read
- line ++; //increment everytime we loop to new line
- //finds the index of where the word starts
- size_t index = result.find("connelly");
- if (result.find("password=")) {
- cout << "Checking password on line: " << (line + 1) << endl;
- found = true;
- }
- //check password if found password field
- if (found) {
- if (index!=string::npos) {
- correct = true;
- cout << "Index: " << (index + 1) << " line: " << line << endl;
- } else if (correct = false) {
- cout << "Invalid password!" << endl;
- } else {
- cerr << "Error: Could not fetch password!" << endl;
- }
- break; //exit the loop
- }
- }
- in.close(); //close the file after we're done with it
- system("pause"); //pause the console so the application does not close right away
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment