#2
songhuirong12012-04-16 21:56
|
代码如下“
程序代码:
#include <QtGui/QApplication>
#include <QFile>
#include <QtDebug>
class Data
{
friend void writeToFile(const QString &);
friend void readFromFile(const QString &);
public:
Data() {}
Data(quint16 id, const QString &name)
{
this->id = id;
this->name = name;
}
private:
quint16 id;
QString name;
};
void writeToFile(const QString &filename)
{
QFile file(filename);
if(!file.open(QIODevice::WriteOnly))
{
qDebug() << "Can not open file" << filename;
return;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_7);
quint32 magicNumber = quint32(0xBE14CF47);
QString names[10] = {"sunsong","jackwu","luckydai","karyyi","jackxu",
"skiffyuan","stanleyyan","fengminhe","junluo","yutaozhang"};
out.writeRawData((char *)&magicNumber, (int)sizeof(quint32));
for(int i = 0; i < 10; ++i)
{
Data data(quint16(i), names[i]);
qDebug() << "Write id:" << quint16(i);
qDebug() << "Write name:" << names[i];
out.writeRawData((char *)&data, (int)sizeof(Data));
}
}
void readFromFile(const QString &filename)
{
QFile file(filename);
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "Can not open file" << filename;
return;
}
QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_7);
quint32 magicNumber;
in.readRawData((char *)&magicNumber, (int)sizeof(quint32));
if(magicNumber != 0xBE14CF47)
{
qDebug() << "File not support!";
return;
}
for(int i = 0; i < 10; ++i)
{
Data data;
in.readRawData((char *)&data, (int)sizeof(Data));
quint16 id = data.id;
qDebug() << "Read id:" << id;
QString name = data.name;
qDebug() << "Read name:" << name;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const QString filename("g:/temp/qtio.dat");
writeToFile(filename);
readFromFile(filename);
return a.exec();
}
但是运行后挂掉了。不知道为何。#include <QFile>
#include <QtDebug>
class Data
{
friend void writeToFile(const QString &);
friend void readFromFile(const QString &);
public:
Data() {}
Data(quint16 id, const QString &name)
{
this->id = id;
this->name = name;
}
private:
quint16 id;
QString name;
};
void writeToFile(const QString &filename)
{
QFile file(filename);
if(!file.open(QIODevice::WriteOnly))
{
qDebug() << "Can not open file" << filename;
return;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_7);
quint32 magicNumber = quint32(0xBE14CF47);
QString names[10] = {"sunsong","jackwu","luckydai","karyyi","jackxu",
"skiffyuan","stanleyyan","fengminhe","junluo","yutaozhang"};
out.writeRawData((char *)&magicNumber, (int)sizeof(quint32));
for(int i = 0; i < 10; ++i)
{
Data data(quint16(i), names[i]);
qDebug() << "Write id:" << quint16(i);
qDebug() << "Write name:" << names[i];
out.writeRawData((char *)&data, (int)sizeof(Data));
}
}
void readFromFile(const QString &filename)
{
QFile file(filename);
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "Can not open file" << filename;
return;
}
QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_7);
quint32 magicNumber;
in.readRawData((char *)&magicNumber, (int)sizeof(quint32));
if(magicNumber != 0xBE14CF47)
{
qDebug() << "File not support!";
return;
}
for(int i = 0; i < 10; ++i)
{
Data data;
in.readRawData((char *)&data, (int)sizeof(Data));
quint16 id = data.id;
qDebug() << "Read id:" << id;
QString name = data.name;
qDebug() << "Read name:" << name;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const QString filename("g:/temp/qtio.dat");
writeToFile(filename);
readFromFile(filename);
return a.exec();
}