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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值