【问题标题】:codes to search within a txt file using QT with c++使用 QT 和 c++ 在 txt 文件中搜索的代码
【发布时间】:2018-11-23 03:32:38
【问题描述】:

我现在正在做一个学校项目,我需要用 QT 构建一个学生管理系统。我有一个 txt 文件,其中列出了学生信息,如下所示:

Studentname accountpassword  studentid

每次用户(学生)输入他们的学生姓名和密码时,我都必须检查姓名和密码是否在数据库中(现在是 txt 文件)。

这就是我现在正在努力解决的问题。我不知道我如何只能逐行搜索。例如,如果第一个学生用户名是jack,他的密码是123456;同时,第二个学生用户名为peter,密码为23567。

如何查看用户名jack和123456是否同时输入?

void MainWindow::on_loginpush_clicked()
{
    QString username = ui -> lineedit_username -> text();
    QString password = ui -> lineedit_password -> text();

    if (username == "admin" && password =="admin")
    {
        Adminmanagment adminview;
        adminview.exec();
    }

    else if( (username != "admin") && (password !="admin"))
        {
            ifstream studentinfo("student.txt");
            if (!studentinfo.is_open())
            {

            }
            else 
            {
                string current_name;
                string current_password;
                string id;
                int numofcourses;
                int gpa;
                char newline;
                char space;
                bool valid =false;
                while((studentinfo>>current_name>>current_password>>id>>numofcourses>>gpa>>noskipws>>newline) &&newline == '\n')
                {   
                    if((current_name == username)&& (current_password== password))
                    {

                    }
                }
            }
}

我的 student.txt 如下所示

姓名密码id课程gpa数

jack 123456 900440123 4 0
testing 987654 900542015 4 2
testing2 8888 900145265 4 2
testing3 8888 900158256 4 0

【问题讨论】:

  • 您需要查看使用 std::getline 以及字符串标记化。
  • 您好,先生,谢谢您的回答,我马上去看看。

标签: c++ qt


【解决方案1】:

未经测试。期望每一行(包括最后一行)以换行符结尾。假设您有 2 个 QStrings usernamepassword

#include <fstream>
#include <string>
#include <cctype>  // std::isspace()

// ...

std::istream& eat_whitespace(std::istream &is) // eats whitespace except '\n'
{
    int ch;
    while (is && (ch = is.peek()) != EOF && ch != '\n'
           && std::isspace(static_cast<char unsigned>(ch)))
    {
        is.get();
    }
    return is;
}

// ...

std::ifstream studentinfo{ "your_file" };
if (!studentinfo.is_open())
    // big trouble

std::string current_name;
std::string current_password;
std::string id;
int numofcourses;
int gpa;
char newline;
bool valid = false;

while ((studentinfo >> current_name >> current_password >> id >> numofcourses >> gpa
                    >> eat_whitespace >> std::noskipws >> newline >> std::skipws )
       && newline == '\n')
{
    if (current_name == username.toLocal8Bit().constData() &&
        current_password == password.toLocal8Bit().constData())
    {
        valid = true;
        break;
    }
}

// valid now true if credentials found.

【讨论】:

  • 这是我第一次同时使用QT和c++,所以有点迷茫。我知道这将在典型的 cpp 文件中工作,但由于某种原因,当我创建一个 cpp 文件并将所有这些代码输入其中时,它会出现很多错误。你们能告诉我为什么QT中的代码与vs中的代码不同吗?
  • @Stefan 你意识到我的回答只说明了原理?无论有没有 Qt,C++ 都没有什么不同。如果您展示了您尝试过的内容以及遇到的错误,则可以为您提供帮助。您可以编辑您的问题以包含相关代码。
  • 感谢您的所有帮助,先生。我刚刚用我有疑问的部分代码编辑了我的问题。所以基本上我有两个 QLineEdit 来收集用户对用户名和密码的输入,我希望检查输入是否与我的 txt 文件中的用户名和 pw 匹配。我现在遇到的错误是我使用QString收集用户名和密码,程序不允许我比较QString和String。
  • something called Semantic Issue 59:38: error: invalid operands to binary expression ('std::__cxx11::string' (aka 'basic_string') and 'QString') 它有一个长长的详细清单。如果您也需要它们,请告诉我。
  • 天哪,它成功了!但是,还有另一个问题。假设我输入了第一组用户名和密码(它们在我的 txt 文件的第一行中)并且代码工作正常。当我输入第二组用户名和密码(它们在我的 txt 文件的第二行中)时,程序无法正常工作。它只是不识别ac和pw。 @剑鱼
【解决方案2】:

您提到您正在使用 Qt。这个问题展示了如何使用 Qfile 逐行读取文件(Read a text file line by line in Qt)。

然后您可以在内置的相等运算符中使用 QStrings 来查看它们是否相同。 http://doc.qt.io/qt-5/qstring.html

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2012-02-05
    • 1970-01-01
    • 2014-02-06
    相关资源
    最近更新 更多