-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-example2.cpp
More file actions
40 lines (33 loc) · 810 Bytes
/
Copy path08-example2.cpp
File metadata and controls
40 lines (33 loc) · 810 Bytes
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
// Дана строка с путём "/home/user/cpp.tar.xz" (Unix).
// Вывести расширение файла (tar.xz)
#include <iostream>
#include <string>
using namespace std;
int main()
{
const char *path = "/home/user/cpp.tar.xz";
// const char *path = "C:\\Program Files\\App\\app.exe";
string str = path;
size_t pos;
pos = str.rfind('/');
if (pos == string::npos)
{
return 1;
}
// cpp.tar.xz
str = str.substr(pos + 1);
if (str.length() == 0)
{
return 1;
}
pos = str.find('.');
if (pos == string::npos)
{
return 1;
}
// tar.xz
str = str.substr(pos + 1);
cout << "Расширение файла: " << str << endl;
}
// $ ./08-example2.out
// Расширение файла: tar.xz