【问题标题】:Getting specific data from a text file in c++从 C++ 中的文本文件中获取特定数据
【发布时间】:2021-05-11 14:14:46
【问题描述】:

我正在做一个登录/注册系统,而我正在努力检查密码是否正确。所以我有一个包含用户数据的文本文件,看起来像这样。

email: email@email.com
username: user123
password: 1234

为了检查用户提供的密码是否正确,我需要将其与文件中的密码进行比较。那么如何从 *.txt 文件中提取密码。我真的不知道从哪里开始。

【问题讨论】:

  • 你知道如何逐行读取文件吗?之后是否需要操作字符串?
  • 好的,谢谢,我要试试。我应该用 .find() 找到我想要的部分还是有其他方法?
  • stackoverflow.com/questions/14265581/… 另外,你应该考虑存储加密的密码

标签: c++ file


【解决方案1】:

你可以看看我找到的这个答案: Reading file line by line
我认为这是一个好的开始!

然后你可以尝试只保留密码:Get substring after specific character

【讨论】:

    【解决方案2】:

    阅读这样的配置文件很有用,您将很快从中受益。通常它是一个非常标准的代码,您可以在其中遍历文件的所有行,读取键值对并存储它们(例如,在std::map<std::string, std::string> 中),以供以后使用。

    这里有一个这样的代码的注释示例:

    #include <fstream>  // ifstream
    #include <sstream>  // stringstream
    #include <iostream> // cout, endl
    #include <iomanip>  // ws
    #include <map>      // map
    
    using namespace std;
    
    int main(){
        map<string, string> configuration; 
        ifstream fin("your_file.txt");
        string line;
        
        while(getline(fin, line)){ // loop through every line in the file
            string key;
            string value;
            stringstream ss(line); // make a stream from the line
            getline(ss, key, ':'); // read key until :
            ss >> ws;              // ignore whitespaces
            getline(ss, value);    // read value until newline
            
            // Store them
            configuration[key] = value;
        }
        cout << "PW: " << configuration["password"] << endl;
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      如果文本文档中包含“电子邮件”、“用户名”和“密码”字样,请先删除它们,使其看起来像这样。

      email@email.com
      user123
      1234
      

      那么如果你把文本文件和源放在同一个文件夹里,这个基本程序应该是一个开始。

      #include <iostream>
      #include <fstream>
      #include <string>
      
      
      
      using std::cout;
      using std::cin;
      using std::ifstream;
      using std::getline;
      using std::endl;
      using std::string;
      
      int main()
      {
      
      ifstream input("info.txt");    //open the text document
      string line;
      //int lineCounter;
      
      
      string mail;
      string password;
      string username;
      
      cout << "Enter your mail: " << endl;
      cin >> mail;
      
      cout << "Enter your password: " << endl;
      cin >> password;
      
      int compareResult;
      
      if (input.is_open())
      {
      
          while(getline (input,line))    //loop through input file line by line
          {
      
              if (line.length() > 0)    //skip empty lines
              {
                  //lineCounter++;    //keeps track of what line you're at
                  compareResult = line.compare(mail);    //compare each line of the document with the entered mail
                  if (compareResult == 0)    //if match..
                  {
                      getline (input,line);    //get next line (username)
                      username = line;    //store username in a variable
                      
                      getline (input,line);    //get next line (password)
                      compareResult = line.compare(password);    //compare password with entered password
                      if (compareResult == 0)    //if match..
                      {
                          cout << "Logged in as " << username << endl;
                      }
                  }
      
              }
      
          }
      
      input.close();
      }
      
      
      return 0;
      }
      

      只需更改代码以匹配您的文本文件的名称,反之亦然,只需尝试并以 user123 身份登录。

      下次您需要找到类似问题的答案时,请搜索“在文本文件中搜索特定 string c++”之类的内容

      我已经很久没有在 c++ 中做过任何事情了,但是这个问题是我注册后看到的第一个问题,所以我觉得有必要, 谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多