-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.h
74 lines (69 loc) · 1.93 KB
/
file.h
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
#ifndef FILE_H
#define FILE_H
#include<QFile>
#include<QDebug>
#include"data.h"
class File{
private:
QFile* file;
QTextStream* stream;
public:
File(QString filename){
file = new QFile(filename);
if(!file->exists()){
file->setFileName(filename);
}
if(!file->open(QFileDevice::ReadWrite| QFileDevice::Text)){
qDebug() << "file open failed";
exit(0);
}
qDebug() << "open success";
stream = new QTextStream(file);
}
void append(Data d){
stream->seek(file->size());
QString str;
str += d.date.toString("yyyy-MM-dd HH:mm:ss");
str.append(";");
str+=QString::number(d.place);
str.append(";");
str += QString::number(d.temper);
str.append(";");
str+= QString::number(d.humid);
str.append("\r\n");
(*stream) << str;
stream->flush();
file->flush();
}
void append(QVector<Data>& vec){
stream->seek(file->size());
for(int i =0;i < vec.size();i++){
QString str;
str += vec[i].date.toString("yyyy-MM-dd HH:mm:ss");
str.append(";");
str+=QString::number(vec[i].place);
str.append(";");
str += QString::number(vec[i].temper);
str.append(";");
str+= QString::number(vec[i].humid);
str.append("\r\n");
(*stream) << str;
}
stream->flush();
file->flush();
}
void read(QVector<Data>& vec){
stream->seek(0);
while(!stream->atEnd()){
QString str = stream->readLine();
QStringList args = str.split(";");
vec.push_back(Data(QDateTime::fromString(args[0],"yyyy-MM-dd HH:mm:ss"),args[1].toInt(),args[2].toInt(),args[3].toInt()));
}
}
~File(){
file->close();
delete file;
delete stream;
}
};
#endif // FILE_H