-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileio.cpp
61 lines (50 loc) · 1.05 KB
/
fileio.cpp
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
#include "fileio.h"
#include <QTextStream>
#include <QFile>
#include <QDir>
FileIO::FileIO(QObject *parent) :
QObject(parent)
{
}
bool FileIO::writeFile(const QString & filename, const QString & data)
{
QFile file;
if (filename.isEmpty() || filename.isNull())
return false;
file.setFileName(filename);
if (!file.open(QIODevice::WriteOnly))
return false;
else {
QTextStream out(&file);
out << data;
file.close();
}
return true;
}
QString FileIO::readFile(const QString & fname)
{
QString fileContent;
QFile file;
if (fname.isEmpty() || fname.isNull())
return QString();
file.setFileName(fname);
if (!file.open(QIODevice::ReadOnly))
return QString();
else {
QString line;
QTextStream t( &file );
do {
line = t.readLine();
fileContent += line;
if (!line.isNull())
fileContent += "\n";
} while (!line.isNull());
file.close();
}
return fileContent;
}
bool FileIO::isDirExists(const QString & dirname)
{
QDir dir(dirname);
return dir.exists();
}