When Qt describes the path Qfile, the absolute path is unknown error

Those invisible characters in my day! ! !
I tossed for 2 days

Thanks: \u202a The magical control character let me know that the path cannot be pasted casually...

The reason is this. I want to try qt's regular expressions to intercept file content. QFile needs to take the file path. As a result, I habitually find the object path from the file attribute security (file attribute -> security -> object name copy )
Insert picture description here
Then I glued it to a button code (regex is not used yet)

void MainWindow::on_pushButton_clicked()
{
    
    
    QString displayString;
    std::string a="‪C:\\Users\\23216\\Desktop\\1.txt";//这是文件属性粘的
//    std::string a="C:\\Users\\23216\\Desktop\\1.txt";//这是手敲的

    QFile file(QString::fromStdString(a));
    qDebug()<<file.errorString();
    qDebug()<<file.fileName();
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    
    
        qDebug()<<"Can't open the file!"<<endl;
    }
    while(!file.atEnd())
    {
    
    
        QByteArray line = file.readLine();
        QString str(line);
        qDebug()<< str;
        displayString.append(str);
    }
}

The result output is like this,
Insert picture description here

When debugging , I found that there is always a strange character in front, so the path is really wrong
Insert picture description here

Later,
I tried the path symbol and confirmed that the "\\"and "/"are the same, except “\”that the mere escape character is incorrect.

Later, I kept experimenting with the function

//QString::fromLocal8Bit() (曾经在Windows上路径的问题这样解决过)
QFile file(QString::fromLocal8Bit("C:\\Users\\23216\\Desktop\\1.txt"));

//string、QString::fromStdString(a)组合,试着去除怪异字符
string a ="‪C:\\Users\\23216\\Desktop\\1.txt"
QString::fromStdString(a)

But this character is not going away... Why do my various searches for absolute paths do not work... It proves that the wrong direction is fatal, and he will make you want fish.

Afterwards, simply search for strange characters on the path during debugging.
\u202a Magical control characters

Then I knocked it again...it became...

This gives me confidence-generally speaking, functions that refer to absolute paths will not go wrong unless the path is wrong.

Guess you like

Origin blog.csdn.net/sinat_27382047/article/details/104732599