将文本数据写入二进制文件,然后从二进制文件中读出,还原为文本数据
1 #include "mainwindow.h"
2 #include <QApplication>
3 #include <QFile>
4 #include <QDataStream>
5
6 int main(int argc, char *argv[])
7 {
8 QApplication a(argc, argv);
9
10 //写二进制文件
11 QFile file("F:/1.txt");
12 if(!file.open(QIODevice::WriteOnly))
13 {
14 qDebug() << "Can't open file for writing";
15 return 0;
16 }
17 QDataStream out(&file);
18 out.setVersion(QDataStream::Qt_5_7);
19 out << int(12) << QByteArray("123");
20 file.close();
21
22 //读二进制文件
23 if (!file.open(QIODevice::ReadOnly))
24 {
25 qDebug() << "Cannot open file for reading: ";
26 return 0;
27 }
28 QDataStream in(&file);
29 in.setVersion(QDataStream::Qt_5_7);
30 int n;
31 QByteArray s;
32 in >> n >> s;
33 file.close();
34 qDebug() << n << s;
35
36 return a.exec();
37 }
运行结果:
1 #include "mainwindow.h"
2 #include <QApplication>
3 #include <QFile>
4 #include <QDebug>
5
6 class T
7 {
8 public:
9 int a;
10 double b;
11 QString c;
12
13 T(int a1, double b1, QString c1);
14
15 friend QDataStream& operator<<(QDataStream &out, const T &t);
16 friend QDataStream& operator>>(QDataStream &in, T &t);
17 };
18
19 T::T(int a1, double b1, QString c1)
20 {
21 a = a1;
22 b = b1;
23 c = c1;
24 }
25
26 QDataStream& operator <<(QDataStream &out, const T &t)
27 {
28 out << t.a << t.b << t.c;
29 return out;
30 }
31
32 QDataStream& operator >>(QDataStream &in, T &t)
33 {
34 int a;
35 double b;
36 QString c;
37 in >> a >> b >> c;
38 t.a = a;
39 t.b = b;
40 t.c = c;
41 return in;
42 }
43
44 int main(int argc, char *argv[])
45 {
46 QApplication a(argc, argv);
47
48 //写二进制文件
49 T t(1, 2, "3");
50 QFile writeFile("1.dat");
51 writeFile.open(QIODevice::WriteOnly);
52 QDataStream out(&writeFile);
53 out << t;
54 writeFile.close();
55
56 //读二进制文件
57 QFile readFile("1.dat");
58 readFile.open(QIODevice::ReadOnly);
59 QDataStream in(&readFile);
60 in >> t;
61 readFile.close();
62 qDebug() << t.a << t.b << t.c;
63
64 return 0;
65 return a.exec();
66 }
运行结果:
1 #include "mainwindow.h"
2 #include <QApplication>
3 #include <QFile>
4 #include <QDebug>
5
6 class T
7 {
8 public:
9 int a;
10 double b;
11 QString c;
12
13 T(int a1, double b1, QString c1);
14 };
15
16 T::T(int a1, double b1, QString c1)
17 {
18 a = a1;
19 b = b1;
20 c = c1;
21 }
22
23
24 int main(int argc, char *argv[])
25 {
26 QApplication a(argc, argv);
27
28 //写文件
29 T t(1, 2, "34");
30 QFile writeFile("1.dat");
31 writeFile.open(QIODevice::WriteOnly);
32 QDataStream out(&writeFile);
33 out.writeRawData((char *)&t, sizeof(t));
34 writeFile.close();
35
36 //读文件
37 QFile readFile("1.dat");
38 readFile.open(QIODevice::ReadOnly);
39 QDataStream in(&readFile);
40 in.readRawData((char *)&t, sizeof(t));
41 readFile.close();
42 qDebug() << t.a << t.b << t.c;
43
44 return 0;
45 return a.exec();
46 }
运行结果:
1 #include "mainwindow.h"
2 #include <QApplication>
3 #include <QFile>
4 #include <QDebug>
5
6 int main(int argc, char *argv[])
7 {
8 QApplication a(argc, argv);
9
10 //写文件
11 int aa = 1;
12 char b = 'c';
13 double c = 2;
14 QFile writeFile("1.dat");
15 writeFile.open(QIODevice::WriteOnly);
16 QDataStream out(&writeFile);
17 out.writeRawData((char *)&aa, sizeof(int));
18 out.writeRawData((char *)&b, sizeof(char));
19 out.writeRawData((char *)&c, sizeof(double));
20 writeFile.close();
21
22 //读文件
23 QFile readFile("1.dat");
24 readFile.open(QIODevice::ReadOnly);
25 QDataStream in(&readFile);
26 in.readRawData((char *)&aa, sizeof(int));
27 in.readRawData((char *)&b, sizeof(char));
28 in.readRawData((char *)&c, sizeof(double));
29 readFile.close();
30 qDebug() << aa << b << c;
31
32 return 0;
33 return a.exec();
34 }
运行结果:
1 #include "mainwindow.h"
2 #include <QApplication>
3 #include <QFile>
4 #include <QDebug>
5 int main(int argc, char *argv[])
6 {
7 QApplication a(argc, argv);
8
9 //写文件
10 int aa = 1;
11 char b = 'c';
12 double c = 2;
13 QFile writeFile("1.dat");
14 writeFile.open(QIODevice::WriteOnly);
15 QDataStream out(&writeFile);
16 out.writeRawData((char *)&aa, sizeof(int));
17 out.writeRawData((char *)&b, sizeof(char));
18 out.writeRawData((char *)&c, sizeof(double));
19 writeFile.close();
20
21 //读文件
22 QFile readFile("1.dat");
23 readFile.open(QIODevice::ReadOnly);
24 QDataStream in(&readFile);
25 char bf[100];
26 in.readRawData((char *)&bf, sizeof(int) + sizeof(char) + sizeof(double));
27 readFile.close();
28
29 memcpy(&aa, bf, sizeof(int));
30 memcpy(&b, bf + 4, sizeof(char));
31 memcpy(&c, bf + 5, sizeof(double));
32 qDebug() << aa << b << c;
33
34 return 0;
35 return a.exec();
36 }
运行结果:
1 #include "mainwindow.h"
2 #include <QApplication>
3 #include <QFile>
4 #include <QDebug>
5
6 int main(int argc, char *argv[])
7 {
8 QApplication a(argc, argv);
9
10 //写文件
11 int aa = 1;
12 char b = 'c';
13 double c = 2;
14 QFile writeFile("1.dat");
15 writeFile.open(QIODevice::WriteOnly);
16 QDataStream out(&writeFile);
17 out.writeRawData((char *)&aa, sizeof(int));
18 out.writeRawData((char *)&b, sizeof(char));
19 out.writeRawData((char *)&c, sizeof(double));
20 writeFile.close();
21
22
23 //读文件
24 QFile readFile("1.dat");
25 readFile.open(QIODevice::ReadOnly);
26 QByteArray by;
27 char *bf;
28 by = readFile.readAll();
29 bf = by.data();
30 readFile.close();
31
32 memcpy(&aa, bf, sizeof(int));
33 memcpy(&b, bf + 4, sizeof(char));
34 memcpy(&c, bf + 5, sizeof(double));
35 qDebug() << aa << b << c;
36
37 return 0;
38 return a.exec();
39 }
运行结果:
1 int main(int argc, char *argv[])
2 {
3 QApplication a(argc, argv);
4
5 //写文件
6 int aa = 1;
7 char b = 'c';
8 double c = 2;
9 QFile writeFile("1.dat");
10 writeFile.open(QIODevice::WriteOnly);
11 QDataStream out(&writeFile);
12 char bbf[100];
13 char *p;
14 p = (char *)&aa;
15 bbf[0] = p[0];
16 bbf[1] = p[1];
17 bbf[2] = p[2];
18 bbf[3] = p[3];
19 p = (char *)&b;
20 bbf[4] = p[0];
21 p = (char *)&c;
22 bbf[5] = p[0];
23 bbf[6] = p[1];
24 bbf[7] = p[2];
25 bbf[8] = p[3];
26 bbf[9] = p[4];
27 bbf[10] = p[5];
28 bbf[11] = p[6];
29 bbf[12] = p[7];
30
31 out.writeRawData(bbf, 13);
32 writeFile.close();
33
34
35 //读文件
36 QFile readFile("1.dat");
37 readFile.open(QIODevice::ReadOnly);
38 QByteArray by;
39 char *bf;
40 by = readFile.readAll();
41 bf = by.data();
42 readFile.close();
43
44 memcpy(&aa, bf, 4);
45 memcpy(&b, bf + 4, 1);
46 memcpy(&c, bf + 5, 8);
47 qDebug() << aa << b << c;
48
49 return 0;
50 return a.exec();
51 }
运行结果:
1 #include "mainwindow.h"
2 #include <QApplication>
3 #include <QFile>
4 #include <QDebug>
5
6 int main(int argc, char *argv[])
7 {
8 QApplication a(argc, argv);
9
10 //写文件
11 int aa = 1;
12 char b = 'c';
13 double c = 2;
14 int d = (int)c;
15 QFile writeFile("1.dat");
16 writeFile.open(QIODevice::WriteOnly);
17 QDataStream out(&writeFile);
18 char bbf[100];
19 char *p;
20 p = (char *)&aa;
21 bbf[0] = p[0];
22 bbf[1] = p[1];
23 bbf[2] = p[2];
24 bbf[3] = p[3];
25 p = (char *)&b;
26 bbf[4] = p[0];
27 p = (char *)&d;
28 bbf[5] = p[0];
29 bbf[6] = p[1];
30 bbf[7] = p[2];
31 bbf[8] = p[3];
32
33 out.writeRawData(bbf, 9);
34 writeFile.close();
35
36
37 //读文件
38 QFile readFile("1.dat");
39 readFile.open(QIODevice::ReadOnly);
40 QByteArray by;
41 char *bf;
42 by = readFile.readAll();
43 bf = by.data();
44 readFile.close();
45
46 int cc;
47 memcpy(&aa, bf, 4);
48 memcpy(&b, bf + 4, 1);
49 memcpy(&cc, bf + 5, 4);
50 qDebug() << aa << b << cc;
51
52 return 0;
53 return a.exec();
54 }
运行结果: