forked from czyt1988/czyBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
181 lines (171 loc) · 3.71 KB
/
main.cpp
File metadata and controls
181 lines (171 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <QCoreApplication>
#include <QFile>
#include <QDebug>
#include <QDataStream>
void demo1QDataStream(QFile& file);
void demo2QDataStream(QFile& file);
void demo3QDataStream(QFile& file);
void demo4QDataStream(QFile& file);
void demo2_1QDataStream(QFile& file);
const QString FILE_NAME = "czyQDataStreamDemo.file";
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile file(FILE_NAME);
//demo1QDataStream(file);
//demo2QDataStream(file);
//demo3QDataStream(file);
demo4QDataStream(file);
//demo2_1QDataStream(file);
return a.exec();
}
void demo1QDataStream(QFile& file)
{
qDebug()<<__FUNCTION__;
if(file.open(QIODevice::WriteOnly))
{
file.seek(0);
QDataStream out(&file);
char data[256];
memset(data,0,256*sizeof(char));
const char* p = "this is QDataStream demo";
strcpy(data,p);
out<<(data,256);
file.close();
}
else
{
qDebug()<<file.errorString();
}
}
void demo2QDataStream(QFile& file)
{
qDebug()<<__FUNCTION__;
if(file.open(QIODevice::WriteOnly))
{
QDataStream out(&file);
char data[256];
memset(data,0,256*sizeof(char));
const char* p = "this is QDataStream demo";
strcpy(data,p);
out.writeBytes(data,256);
file.close();
}
else
{
qDebug()<<file.errorString();
}
if(file.open(QIODevice::ReadOnly))
{
QDataStream out(&file);
char *data;
uint len;
out.readBytes(data,len);
if(len > 0)
{
qDebug()<<data;
delete[] data;
}
file.close();
}
else
{
qDebug()<<file.errorString();
}
}
void demo3QDataStream(QFile& file)
{
qDebug()<<__FUNCTION__;
if(file.open(QIODevice::WriteOnly))
{
QDataStream out(&file);
char data[256];
memset(data,0,256*sizeof(char));
const char* p = "this is QDataStream demo";
strcpy(data,p);
out.writeRawData(data,256);
file.close();
}
else
{
qDebug()<<file.errorString();
}
if(file.open(QIODevice::ReadOnly))
{
QDataStream out(&file);
char data[256];
memset(data,1,256*sizeof(char));
out.readRawData(data,256);
qDebug()<<data;
file.close();
}
else
{
qDebug()<<file.errorString();
}
}
void demo4QDataStream(QFile& file)
{
qDebug()<<__FUNCTION__;
if(file.open(QIODevice::WriteOnly))
{
QDataStream out(&file);
char data[256];
memset(data,0,256*sizeof(char));
const wchar_t * p = L"this is QDataStream demo 这里还有中文!";
strcpy(data,p);
out.writeRawData(data,256);
file.close();
}
else
{
qDebug()<<file.errorString();
}
if(file.open(QIODevice::ReadOnly))
{
QDataStream out(&file);
char data[256];
memset(data,1,256*sizeof(char));
out.readRawData(data,256);
qDebug()<<data;
file.close();
}
else
{
qDebug()<<file.errorString();
}
}
void demo2_1QDataStream(QFile& file)
{
qDebug()<<__FUNCTION__;
if(file.open(QIODevice::WriteOnly))
{
QDataStream out(&file);
int i = 0;
for (;i<10;++i)
{
out<<i;
}
file.close();
}
else
{
qDebug()<<file.errorString();
}
if(file.open(QIODevice::ReadOnly))
{
QDataStream out(&file);
int i = 0;
for (;i<10;++i)
{
int data;
out>>data;
qDebug() << data;
}
file.close();
}
else
{
qDebug()<<file.errorString();
}
}