QFile QTextStream操作文件

    QFile file("E:\\QT_Practice\\FileInfo\\dome.txt");

    //文件写入
    //int data = 100000000;
    double db = 172.16801;

    if(file.open(QFile::WriteOnly | QFile::Truncate))
    {
        QTextStream str(&file);
       //str.setRealNumberPrecision(3);
       str.setFieldWidth(10);

       str<<QString("PI:")<<left<<hex<<showbase<<db;
       file.close();
    }
     else    qDebug()<<file.errorString();

    //读取文件
    if(file.open(QIODevice::ReadOnly))
    {
        int w;
        char buffer[128];

        w = file.readLine(buffer,sizeof(buffer));

        if(w)
        {
            qDebug()<<"长度:"<<w<<"内容"<<buffer<<endl;
            file.close();
        }
    }

 

posted @ 2022-03-25 15:58  乐吴  阅读(64)  评论(0编辑  收藏  举报