-
Notifications
You must be signed in to change notification settings - Fork 1
/
files.cpp
49 lines (39 loc) · 999 Bytes
/
files.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
#include "files.h"
#include "QFile"
#include "QDir"
#include "QTextStream"
using namespace std;
Files::Files()
{
}
// Read file contents
QString Files::read(QString filename){
QString content = "";
QFile file(filename);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine() + "\n";
content += line;
}
}
return content;
}
// Write String Content To File
void Files::write(QString filename, QString content){
QFile file(filename);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)){
QTextStream out(&file);
out << content;
}
}
// Return Users Home Directory
QString Files::getHomeDir(){
QString homeDir = QDir::homePath();
return homeDir;
}
QString Files::getDirectory(QString filepath){
int lastIndex = filepath.lastIndexOf("/");
filepath.chop(filepath.length() - lastIndex);
return filepath;
}