-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2.cpp
More file actions
105 lines (93 loc) · 2.88 KB
/
main2.cpp
File metadata and controls
105 lines (93 loc) · 2.88 KB
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
99
100
101
102
103
104
105
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#define GetCurrentDir getcwd
#endif
std::string get_current_dir() {
char buff[FILENAME_MAX];
GetCurrentDir(buff, FILENAME_MAX);
std::string current_working_dir(buff);
return current_working_dir;
}
bool is_directory(const std::string& path) {
#ifdef _WIN32
DWORD ftyp = GetFileAttributesA(path.c_str());
if (ftyp == INVALID_FILE_ATTRIBUTES)
return false;
if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
return true;
#else
struct stat statbuf;
if (stat(path.c_str(), &statbuf) != 0)
return false;
return S_ISDIR(statbuf.st_mode);
#endif
return false;
}
std::vector<std::string> list_directory(const std::string& path) {
std::vector<std::string> result;
#ifdef _WIN32
WIN32_FIND_DATA find_data;
HANDLE hFind = FindFirstFile((path + "\\*").c_str(), &find_data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
result.push_back(find_data.cFileName);
} while (FindNextFile(hFind, &find_data) != 0);
FindClose(hFind);
}
#else
DIR* dir = opendir(path.c_str());
if (dir != nullptr) {
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
result.push_back(entry->d_name);
}
closedir(dir);
}
#endif
return result;
}
void print_directory_tree(const std::string& path, const std::string& prefix = "") {
std::vector<std::string> items = list_directory(path);
std::sort(items.begin(), items.end());
for (size_t i = 0; i < items.size(); ++i) {
if (items[i] == "." || items[i] == "..") continue;
bool is_last_item = (i == items.size() - 1);
std::string new_prefix = prefix + (is_last_item ? "`-- " : "|-- ");
std::cout << new_prefix << items[i] << std::endl;
if (is_directory(path + "/" + items[i])) {
print_directory_tree(path + "/" + items[i],
prefix + (is_last_item ? " " : "| "));
}
}
}
int main() {
std::string current_dir = get_current_dir();
std::cout << "Current working directory: " << current_dir << std::endl;
std::cout << "Directory tree:" << std::endl;
print_directory_tree(current_dir);
std::string file_path = "resources/subfolder1/r4.txt";
std::cout << "\nAttempting to open file: " << file_path << std::endl;
std::ifstream file(file_path);
if (file.is_open()) {
std::cout << "File contents:" << std::endl;
std::string line;
while (std::getline(file, line)) {
std::cout << line << std::endl;
}
file.close();
} else {
std::cerr << "Unable to open file: " << file_path << std::endl;
}
return 0;
}