This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileinfo.cpp
98 lines (87 loc) · 1.49 KB
/
fileinfo.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
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
#include "fileinfo.h"
//去除字符串中的空格
std::string file_trim(std::string &s)
{
std::string tmp;
if (!s.empty())
{
int i=0;
int flag=0;
while(s[i]!='\0')
{
if(s[i]!=' ')
{
tmp=tmp+s[i];
flag=0;
}
else
{
if(flag==0)
{
flag=1;
tmp=tmp+':';
}
}
i++;
}
}
return tmp;
}
// ---- get file info ---- //
std::vector<File> getFileInfo() {
std::vector<File> files;
FILE *fp;
char buf[512];
fp = popen("df -h", "r");
fgets(buf,512, fp);
std::string value;
while(fgets(buf,512,fp))
{
File file;
int pos;
std::string s(buf);
s=file_trim(s);
pos=s.find(":");
value=s.substr(0,pos);
s=s.substr(pos+1);
// cout<<s<<endl;
file.filesys=value;
pos=s.find(":");
value=s.substr(0,pos);
s=s.substr(pos+1);
// cout<<s<<endl;
file.size=value;
pos=s.find(":");
value=s.substr(0,pos);
s=s.substr(pos+1);
file.used=value;
pos=s.find(":");
value=s.substr(0,pos);
s=s.substr(pos+1);
file.avail=value;
pos=s.find(":");
value=s.substr(0,pos);
s=s.substr(pos+1);
file.userdperc=value;
pos=s.find(":");
value=s.substr(0,pos);
s=s.substr(pos+1);
file.mounted=value;
// file.tostring();
files.push_back(file);
}
pclose(fp);
return files;
}
/* int main()
{
printf("=== file infomation ===\n");
vector<File> files;
files=getFileInfo();
for (int i = 0; i<files.size(); ++i)
{
files[i].tostring();
}
// file.tostring();
return 0;
} */