#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile data("QFile.txt");
if(data.open(QFile::WriteOnly|QFile::Truncate))
{
QTextStream out(&data);
out<<QObject::tr("aaa:")<<qSetFieldWidth(10)<<left<<90<<endl;
}
return a.exec();
}
QFile data("QFile.txt");
data.open(QFile::WriteOnly|QFile::Truncate)
QTextStream out(&data);
binary:
void MainWindow::fileFun()
{
QFile file("binary.dat");
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QDataStream out(&file); //将数据序列化
out << QString(tr("this_is_a_tiger:")); //字符串序列化
out << QDate::fromString("2020/09/25", "yyyy/MM/dd");
out << (qint32)19; //整数序列化
file.close();
file.setFileName("binary.dat");
if(!file.open(QIODevice::ReadOnly))
{
qDebug()<< "error!";
return;
}
QDataStream in(&file); //从文件中读出数据
QString name;
QDate birthday;
qint32 age;
in >> name >> birthday >> age; //获取字符串和整数
qDebug() << name << birthday << age;
file.close();
}
QFile file("binary.dat");
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QDataStream out(&file);
file.close();