QT 读写文件 create_file read_file del_file

创建文件 //create_file(QString str)
str 写到文件里面


void MainWindow::create_file(QString str)
{

    QString AppPath = QApplication::applicationFilePath();
    qDebug() << "AppPath:" << AppPath;

    QString  openssl_path_dir = AppPath;
    openssl_path_dir = openssl_path_dir.replace(QString("cmd_openssl.exe"), QString("openssl/bin/"));
    qDebug() << "openssl_path_dir:" << openssl_path_dir;

    QString fileName=openssl_path_dir + "psw_tmp.txt";
    qDebug() << "fileName:" << fileName;
    glb_psw_tmp_file_path = fileName;
    QFile f(fileName);

    if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug()  << "Open failed.";
        QMessageBox::warning(this,"file error","can't open",QMessageBox::Yes);
    }

    QTextStream in(&f);
    in << str;//没有回车
    in << str << endl;//有回车
    f.close();
}
/*
AppPath: "E:/Qt/leo_qt/build-cmd_openssl-Desktop_Qt_5_8_0_MinGW_32bit-Release/release/cmd_openssl.exe"
openssl_path_dir: "E:/Qt/leo_qt/build-cmd_openssl-Desktop_Qt_5_8_0_MinGW_32bit-Release/release/openssl/bin/"
fileName: "E:/Qt/leo_qt/build-cmd_openssl-Desktop_Qt_5_8_0_MinGW_32bit-Release/release/openssl/bin/psw_tmp.txt"
*/

读文件 read_file(QString file_path)

QString MainWindow::read_file(QString file_path)
{
    QFile f(file_path);
    if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug()  << "Open failed.";
        QMessageBox::warning(this,"sdf","can't open",QMessageBox::Yes);
    }

    QTextStream txtOutput(&f);
    QString lineStr;
    QString all_line_str;
    while(!txtOutput.atEnd())
    {
        lineStr = txtOutput.readLine();
        all_line_str += lineStr;
       // cout << lineStr << endl;
        qDebug()  << "lineStr:" << lineStr;
    }
    qDebug()  << "all_lineStr:" << all_line_str;

    qDebug()  << file_path;
    f.close();
   // f.remove(file_path);
    return all_line_str;
}

//删除临时文件

    QString fileName=glb_psw_tmp_file_path;
    QFile file;
    file.remove(fileName);

猜你喜欢

转载自blog.csdn.net/linbounconstraint/article/details/74529356