Qt---多种方式读写二进制文件

时间:2022-05-22 17:18:51
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDataStream>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//写二进制文件
QFile file("F:/1.txt");
if(!file.open(QIODevice::WriteOnly))
{
qDebug() << "Can't open file for writing";
return 0;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_5_7);
out << int(12) << QByteArray("123");
file.close();

//读二进制文件
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "Cannot open file for reading: ";
return 0;
}
QDataStream in(&file);
in.setVersion(QDataStream::Qt_5_7);
int n;
QByteArray s;
in >> n >> s;
file.close();
qDebug() << n << s;

return a.exec();
}

运行结果:
Qt---多种方式读写二进制文件

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>

class T
{
public:
int a;
double b;
QString c;

T(int a1, double b1, QString c1);

friend QDataStream& operator<<(QDataStream &out, const T &t);
friend QDataStream& operator>>(QDataStream &in, T &t);
};

T::T(int a1, double b1, QString c1)
{
a = a1;
b = b1;
c = c1;
}

QDataStream& operator <<(QDataStream &out, const T &t)
{
out << t.a << t.b << t.c;
return out;
}

QDataStream& operator >>(QDataStream &in, T &t)
{
int a;
double b;
QString c;
in >> a >> b >> c;
t.a = a;
t.b = b;
t.c = c;
return in;
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//写二进制文件
T t(1, 2, "3");
QFile writeFile("1.dat");
writeFile.open(QIODevice::WriteOnly);
QDataStream out(&writeFile);
out << t;
writeFile.close();

//读二进制文件
QFile readFile("1.dat");
readFile.open(QIODevice::ReadOnly);
QDataStream in(&readFile);
in >> t;
readFile.close();
qDebug() << t.a << t.b << t.c;

return 0;
return a.exec();
}

运行结果:
Qt---多种方式读写二进制文件

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>

class T
{
public:
int a;
double b;
QString c;

T(int a1, double b1, QString c1);
};

T::T(int a1, double b1, QString c1)
{
a = a1;
b = b1;
c = c1;
}


int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//写文件
T t(1, 2, "34");
QFile writeFile("1.dat");
writeFile.open(QIODevice::WriteOnly);
QDataStream out(&writeFile);
out.writeRawData((char *)&t, sizeof(t));
writeFile.close();

//读文件
QFile readFile("1.dat");
readFile.open(QIODevice::ReadOnly);
QDataStream in(&readFile);
in.readRawData((char *)&t, sizeof(t));
readFile.close();
qDebug() << t.a << t.b << t.c;

return 0;
return a.exec();
}

运行结果:
Qt---多种方式读写二进制文件

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//写文件
int aa = 1;
char b = 'c';
double c = 2;
QFile writeFile("1.dat");
writeFile.open(QIODevice::WriteOnly);
QDataStream out(&writeFile);
out.writeRawData((char *)&aa, sizeof(int));
out.writeRawData((char *)&b, sizeof(char));
out.writeRawData((char *)&c, sizeof(double));
writeFile.close();

//读文件
QFile readFile("1.dat");
readFile.open(QIODevice::ReadOnly);
QDataStream in(&readFile);
in.readRawData((char *)&aa, sizeof(int));
in.readRawData((char *)&b, sizeof(char));
in.readRawData((char *)&c, sizeof(double));
readFile.close();
qDebug() << aa << b << c;

return 0;
return a.exec();
}

运行结果:
Qt---多种方式读写二进制文件

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//写文件
int aa = 1;
char b = 'c';
double c = 2;
QFile writeFile("1.dat");
writeFile.open(QIODevice::WriteOnly);
QDataStream out(&writeFile);
out.writeRawData((char *)&aa, sizeof(int));
out.writeRawData((char *)&b, sizeof(char));
out.writeRawData((char *)&c, sizeof(double));
writeFile.close();

//读文件
QFile readFile("1.dat");
readFile.open(QIODevice::ReadOnly);
QDataStream in(&readFile);
char bf[100];
in.readRawData((char *)&bf, sizeof(int) + sizeof(char) + sizeof(double));
readFile.close();

memcpy(&aa, bf, sizeof(int));
memcpy(&b, bf + 4, sizeof(char));
memcpy(&c, bf + 5, sizeof(double));
qDebug() << aa << b << c;

return 0;
return a.exec();
}

Qt---多种方式读写二进制文件

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//写文件
int aa = 1;
char b = 'c';
double c = 2;
QFile writeFile("1.dat");
writeFile.open(QIODevice::WriteOnly);
QDataStream out(&writeFile);
out.writeRawData((char *)&aa, sizeof(int));
out.writeRawData((char *)&b, sizeof(char));
out.writeRawData((char *)&c, sizeof(double));
writeFile.close();


//读文件
QFile readFile("1.dat");
readFile.open(QIODevice::ReadOnly);
QByteArray by;
char *bf;
by = readFile.readAll();
bf = by.data();
readFile.close();

memcpy(&aa, bf, sizeof(int));
memcpy(&b, bf + 4, sizeof(char));
memcpy(&c, bf + 5, sizeof(double));
qDebug() << aa << b << c;

return 0;
return a.exec();
}

Qt---多种方式读写二进制文件

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//写文件
int aa = 1;
char b = 'c';
double c = 2;
QFile writeFile("1.dat");
writeFile.open(QIODevice::WriteOnly);
QDataStream out(&writeFile);
char bbf[100];
char *p;
p = (char *)&aa;
bbf[0] = p[0];
bbf[1] = p[1];
bbf[2] = p[2];
bbf[3] = p[3];
p = (char *)&b;
bbf[4] = p[0];
p = (char *)&c;
bbf[5] = p[0];
bbf[6] = p[1];
bbf[7] = p[2];
bbf[8] = p[3];
bbf[9] = p[4];
bbf[10] = p[5];
bbf[11] = p[6];
bbf[12] = p[7];

out.writeRawData(bbf, 13);
writeFile.close();


//读文件
QFile readFile("1.dat");
readFile.open(QIODevice::ReadOnly);
QByteArray by;
char *bf;
by = readFile.readAll();
bf = by.data();
readFile.close();

memcpy(&aa, bf, 4);
memcpy(&b, bf + 4, 1);
memcpy(&c, bf + 5, 8);
qDebug() << aa << b << c;

return 0;
return a.exec();
}

运行结果:
Qt---多种方式读写二进制文件

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//写文件
int aa = 1;
char b = 'c';
double c = 2;
int d = (int)c;
QFile writeFile("1.dat");
writeFile.open(QIODevice::WriteOnly);
QDataStream out(&writeFile);
char bbf[100];
char *p;
p = (char *)&aa;
bbf[0] = p[0];
bbf[1] = p[1];
bbf[2] = p[2];
bbf[3] = p[3];
p = (char *)&b;
bbf[4] = p[0];
p = (char *)&d;
bbf[5] = p[0];
bbf[6] = p[1];
bbf[7] = p[2];
bbf[8] = p[3];

out.writeRawData(bbf, 9);
writeFile.close();


//读文件
QFile readFile("1.dat");
readFile.open(QIODevice::ReadOnly);
QByteArray by;
char *bf;
by = readFile.readAll();
bf = by.data();
readFile.close();

int cc;
memcpy(&aa, bf, 4);
memcpy(&b, bf + 4, 1);
memcpy(&cc, bf + 5, 4);
qDebug() << aa << b << cc;

return 0;
return a.exec();
}

运行结果:
Qt---多种方式读写二进制文件