QDataStream类用于将任意常见类型的数据以二进制写入由QFile指定的磁盘文件中。同时也可以按写入的顺序读取这些类型的数据,并且还可以用在网络通信中传输消息,读取和传输都非常方便
//将数据写入文件中,但是数据会覆盖掉之前的
void MainWindow::on_pushButton_clicked()
{
QFile file("test.txt");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
QString str = "aaaa";
QString str1 = "1111";
QString str2 = "2222";
out << str << str1 << str2;
file.close();
}
//从文件中读出数据
void MainWindow::on_pushButton_2_clicked()
{
QFile file("test.txt");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QString str,str1,str2;
in >> str >> str1 >> str2;
file.close();
ui->textEdit->append(str);
ui->textEdit->append(str1);
ui->textEdit->append(str2);
}
//将要发送的数据包转为QDataStream 后发送
void novideo_send(uchar flg,QString local_ip,QString remote_ip,quint16 remote_port)
{
QByteArray datagram;
QDataStream in(&datagram,QIODevice::WriteOnly);
unsigned short nMsgHead,nMsgLength,nMsgFlg,nMsgTail;
nMsgHead= 0x66bb;
nMsgLength = 0;
nMsgFlg = 0x0003;//端口信息
nMsgTail = 0x77cc;
quint64 fill = 0;
int local_ip1 = 0, local_ip2 = 0, local_ip3 = 0, local_ip4 = 0;
if ( sscanf(local_ip.toLatin1().data(),"%d.%d.%d.%d",&local_ip1,&local_ip2,&local_ip3,&local_ip4) < 0 )
{
return ;
}
in<<nMsgHead<<nMsgLength<<nMsgFlg<<flg<<uchar(local_ip1)<<uchar(local_ip2)<<uchar(local_ip3)<<uchar(local_ip4)<<nMsgTail<<fill<<fill<<fill<<fill<<fill<<fill<<fill<<fill;
udp_send->writeDatagram(datagram, QHostAddress(remote_ip), 8200);
}
//将接收到的数据包通过QDataStream解析出来
void novideo_receive(QByteArray datagram)
{
QDataStream in(&datagram,QIODevice::ReadWrite);
in.setVersion(QDataStream::Qt_5_1);
unsigned short nMsgHead,nMsgLength,nMsgFlg,nMsgTail;
uchar flg;
uchar local_ip1 = 0, local_ip2 = 0, local_ip3 = 0, local_ip4 = 0;
in>>nMsgHead>>nMsgLength>>nMsgFlg>>flg>>local_ip1>>local_ip2>>local_ip3>>local_ip4>>nMsgTail;
QString ipstr,ipstr1;
ipstr = QString("%1.%2.%3.%4").arg(local_ip1).arg(local_ip2).arg(local_ip3).arg(local_ip4);
}
QDataStream类实现了将QIODevice的二进制数据串行化。一个数据流就是一个二进制编码信息流 ,它完全独立于主机的操作系统,cpu和字节顺序。数据流可以读写未编码的原始二进制数据。QDataStream类可以实现C++基本数据类型的串行化,比如char,short int,int,char*等。串行化更复杂的数据是通过将数据分解为基本的数据类型来完成的。
#include <QCoreApplication>
#include <QFile>
#include <QDataStream>
#include <QDebug>
#include <QFileInfo>
struct MyData
{
int num;
char ch[2];
bool b;
double d;
qint16 count;
};//4+2+1+8+2=1
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//将二进制数据写入到数据流中
QFile file("myFile.dat");
QFileInfo info(file);
qDebug()<<info.absoluteFilePath();
if(!file.open(QIODevice::WriteOnly))
{
qDebug()<<file.errorString();
}
QDataStream out(&file);
out<<QString("the answer is");
out<<(qint32)42;
file.flush();
file.close();
//从数据流中读取二进制数据
if(!file.open(QIODevice::ReadOnly))
{
qDebug()<<file.errorString();
}
QDataStream in(&file);
QString str;
qint32 num;
in>>str>>num;
file.close();
//写入结构体
QFile file2;
file2.setFileName("myFile2.dat");
if(!file2.open(QIODevice::WriteOnly))
{
qDebug()<<file.errorString();
}
MyData data;
data.b = true;
data.num = 64;
data.count = 16;
data.ch[0] = 'a';
data.ch[1] = 'b';
data.d = 3.1415926;
QDataStream out2(&file2);
out2.writeBytes((const char*)&data,sizeof(MyData));
file2.flush();
file2.close();
//读取结构体
if(!file2.open(QIODevice::ReadOnly))
{
qDebug()<<file.errorString();
}
QDataStream in2(&file2);
char *pBuf = new char[sizeof(MyData)];
uint count;
in2.readBytes(pBuf,count);
MyData* pData = (MyData*)pBuf;
qDebug()<<pData->b;
qDebug()<<pData->ch;
qDebug()<<pData->count;
qDebug()<<pData->d;
qDebug()<<pData->num;
file2.close();
return a.exec();
}