qt 读写文件

来源:互联网 发布:js对象私有属性 编辑:程序博客网 时间:2024/05/20 14:26

1)按行读取

QString apppath = QApplication::applicationDirPath() + "/host.txt";
//    qDebug()<<"apppath = "<<apppath;
QFile hostfile(apppath);
if (hostfile.exists()){
hostfile.open(QIODevice::ReadOnly);
QTextStream stream(&hostfile);
program.hostURL = stream.readLine();
program.basePath = stream.readLine();
program.htmlUrl = stream.readLine();
program.demensionUrl = stream.readLine();
//        qDebug()<<"program.hostURL="<<program.hostURL;
//        qDebug()<<"program.hostPort="<<program.hostPort;
hostfile.close();
}

2)写文件

QFile accountFile(QApplication::applicationDirPath() + "/account.txt");

accountFile.open(QIODevice::WriteOnly)

if (!accountFile.write(accountName.toStdString().c_str()))
{
//保存错误
}
accountFile.close();

原创粉丝点击