Home > Mobile >  Want to ask why I Qt this code is unable to read from the specified file
Want to ask why I Qt this code is unable to read from the specified file

Time:10-11

# include "menu. H"
# include "ui_menu. H"
# include "about h"
# include "studentfunc. H"
#include
#include
#include

Menu: : Menu (QWidget * parent) :
QDialog (parent),
UI (new UI: : Menu)
{
The UI - & gt; SetupUi (this);
If (readFromFile ()==1) {
QMessageBox: : critical (this, "Wrong", "Fail to open the file");
This - & gt; Close ();
}
The else {
QMessageBox: : warning (this, "hello", "the File read successfully");
}
}

Menu: : ~ Menu ()
{
Delete the UI;
}

Void Menu: : on_pushButton_2_clicked ()
{
This - & gt; Close ();
}

Int Menu: : readFromFile ()
{
QFile file (" message. TXT ");
if(! The file open (QIODevice: : ReadOnly | QIODevice: : Text))
{
return -1;
}
QTextStream (in & amp; The file);
while(! In. AtEnd ())
{
QString line=in readLine ();
Stu_lines. Append (line);
}
file.close();
return 0;
}

Void Menu: : on_Loginbtn_clicked ()
{
QString line_account="admin", line_password="admin"
Account=this - & gt; The UI - & gt; Line_account - & gt; The text ();
Password=this - & gt; The UI - & gt; Line_password - & gt; The text ();
//if (readFromFile ()==1)
//{
//QMessageBox: : warning (nullptr, "warning", "Defeat");
////this - & gt; Close ();
//}


int i=0;
for(i=0; i{
QString line=stu_lines. At (I);
The line=line. Trimmed ();
QStringList linesplit=line. The split (" ");
If (account==linesplit. At (1) & amp; & Password==linesplit. Ats (6))
{
This - & gt; Hide ();
Cwhstudentfunc. The exec ();
}
}
}

CodePudding user response:

Reason: the file path with executable file path at the same level rather than the source file

CodePudding user response:

See the reference others
https://blog.csdn.net/me_badman/article/details/54864100

CodePudding user response:

That is a relative path, you just write the file name is executable file directory, at the same level "./message. TXT "will be superior to executable directory to find, to the relative position of the specified file, meet next time can have a look at the return values of failed to open the file just know what's the problem

CodePudding user response:

QFile file (" message. TXT ");
Should be a path problem, change to be an absolute path! Or a file does not exist,

CodePudding user response:

Determine the current directory file does not exist, then watch loading error message,
  •  Tags:  
  • Qt
  • Related