不得不说Qt很强大,它完美的支持对文件的操作,说等到对文件的操作就要涉及到两个基本的类,QFile 和QTextStream,前者负责文件的打开,关闭 等等一些执行的操作,后者是对文件流的操作,对文件内容的读写,都要通过它来完成。

void setOs::getNetcfg()
{
    QString s;
    QFile file("net.cfg");
    if(file.open(QIODevice::ReadOnly))
    {
        QTextStream stream(&file);
        QString line;
        while(!stream.atEnd())
        {
            line = stream.readLine();
            if(line.left(13)== QString("export IPADDR"))
            {
                ui->IPaddr->setText(line.section("=",1,1));
            }
            else if(line.left(14)== QString("export NETMASK"))
            {
                ui->subnetMask->setText(line.section("=",1,1));
            }
            else if(line.left(10)== QString("export NDS"))
            {
                ui->DNS->setText(line.section("=",1,1));
            }

        }
        file.close();
    }

}

QTextStream 可以按行读取,另外我们对文件可以这样写入

void setOs::setNetcfg()
{
    QFile file("net.cfg");
    if(file.open(QIODevice::WriteOnly))
    {
        qDebug()<<"open sucess";
        QTextStream stream(&file);
        stream << "export IPADDR=" << ui->IPaddr->text()<<"\n";
    }
}

很方便的。。

相关文章:

  • 2022-12-23
  • 2021-05-29
  • 2021-10-31
  • 2021-04-18
  • 2022-01-19
  • 2022-02-22
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-13
  • 2021-07-28
  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案