文件写入和读取--c++和qt

1.写入数据到文件

    //writetext
    QFile file("./test.txt");
    file.open( QIODevice::ReadWrite | QIODevice::Append);
    QTextStream out(&file);
    if(!ui->lineEdit->text().isEmpty() && !ui->lineEdit_2->text().isEmpty()){
        out << ui->lineEdit->text();
        out << ":";
        out << ui->lineEdit_2->text();
        out << "\n";
    }
    file.close();

文件写入和读取--c++和qt_第1张图片

2.从文件中读取数据

    QFile file("./test.txt");
    if(!file.open(QIODevice::ReadOnly))
    {
         return;
    }
    QTextStream in(&file);
    while(!in.atEnd()){
        QString onecount = in.readLine();
        QStringList userpwd = onecount.split(':');
        qDebug() << userpwd.at(0) << "--" << userpwd.at(1);
    }
    file.close();

这里写图片描述

3.通过单个json对象形式写入

//头文件
#include
#include
#include
#include
#include
#include
//写入
    QFile file("./test.txt");
    file.open( QIODevice::ReadWrite | QIODevice::Append);
    QTextStream out(&file);
    if(!ui->lineEdit->text().isEmpty() && !ui->lineEdit_2->text().isEmpty()){
        QJsonObject json;
        json.insert("name",ui->lineEdit->text());
        json.insert("pwd",ui->lineEdit_2->text());

        QJsonDocument document1;
        document1.setObject(json);
        QByteArray byteArray = document1.toJson(QJsonDocument::Compact);
        QString strJson(byteArray);
        out << strJson;
        out << "\n";
    }

    file.close();
//读取
    QFile file("./test.txt");
    if(!file.open(QIODevice::ReadOnly))
    {
         return;
    }
    QTextStream in(&file);

    while(!in.atEnd()){
        QString onecount = in.readLine();
        QByteArray byteArray = onecount.toLocal8Bit();
        QJsonParseError jsonError;
        QJsonDocument doucment = QJsonDocument::fromJson(byteArray,&jsonError);

        if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)){
            if (doucment.isObject()) {
                QJsonObject object = doucment.object();
                if (object.contains("name")) {
                    QJsonValue value = object.value("name");
                    if (value.isString()){
                        QString strName = value.toString();
                        qDebug() << strName;
                    }
                }
                if (object.contains("pwd")) {
                    QJsonValue value = object.value("pwd");
                    if (value.isString()){
                        QString strName = value.toString();
                        qDebug() << strName;
                    }
                }
            }
        }
    }
    file.close();

4.写入json数组

//写入
    QJsonObject json;
    json.insert("name","aaa");
    json.insert("pwd","123");
    QJsonObject json1;
    json1.insert("name","bbb");
    json1.insert("pwd","111");

    QJsonArray arr;
    arr.append(json);
    arr.append(json1);

    QJsonDocument document1;
    document1.setArray(arr);
    QByteArray byteArray = document1.toJson(QJsonDocument::Compact);
    QString strJson(byteArray);
//读取
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(byteArray,&jsonError);

    if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)){
        if(doucment.isArray()){
            QJsonArray temp = doucment.array();
            for(int i=0; i
                QJsonValue tempvalue = temp.at(i);
                if(tempvalue.isObject()){
                    QJsonObject object = tempvalue.toObject();
                    if (object.contains("name")) {
                        QJsonValue value = object.value("name");
                        if (value.isString()){
                            QString strName = value.toString();
                            qDebug() << strName;
                        }
                    }
                }

            }
        }
    }

5.从文件中读取内容

    QFile file("./textFile.txt");
    if(file.open(QIODevice::ReadOnly))
    {
       char buffer[2048];
       //读取所有内容
       //QByteArray bytearr = file.readAll();
       // qDebug()<<  "all" << bytearr;
       //逐行读取内容
       while(!file.atEnd()){
           qint64 lineLen = file.readLine(buffer,sizeof(buffer));
           qDebug()<< "b1" <if(lineLen!=-1)
           {
               qDebug()<< "b1" <file.close();

文件写入和读取--c++和qt_第2张图片这里写图片描述

6.ini文件写入读取

#include 

QSettings *config = new QSettings("./test.ini", QSettings::IniFormat);

void writeToFile(const QString groupName,const QString key,const QString value)
{
    config->beginGroup(groupName);
    config->setValue(key,value);
    config->endGroup();
}

QString readFromFile(const QString groupName,const QString key)
{
    QString filePath = groupName + "/" + key;
    QString objectString = config->value(filePath).toString();

    return objectString;
}

//写入
config->writeToFile("ip","ip","192.168.1.1");
config->writeToFile("next","nexus5","1999");
//读取
QString temp = config->readFromFile("ip","ip");

文件内容:
这里写图片描述

7.将char*数组写入到文件

    //writetext
    QFile file("./test.cpp");
    file.open( QIODevice::WriteOnly);
    file.write((char*)pBuffer, lBufferSize);
    file.close();

你可能感兴趣的:(c++)