-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_io.h
68 lines (59 loc) · 1.98 KB
/
file_io.h
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
namespace file_io
{
// Read in a single file into the buffer
std::string readFile(const std::string& file_name)
{
std::string buffer;
std::ifstream f(file_name);
f.seekg(0, std::ios::end);
buffer.resize(f.tellg());
f.seekg(0);
f.read(buffer.data(), buffer.size());
return buffer;
}
// Get input from user
void getInput(std::string& buffer, int& min_word, int& max_word, char& order)
{
int num_files;
std::string file_path;
// Get number of text files to process
std::cout << "Enter the number of text files: " << std::flush;
std::cin >> num_files;
// Get path of each text file
for (int i = 1; i <= num_files; ++i)
{
std::cout << "Enter the path of text file " << i << ": ";
std::cin >> file_path;
buffer += readFile(file_path);
buffer += ' ';
}
// Get min and max length of word
std::cout << "Enter the minimum length of word to consider: ";
std::cin >> min_word;
std::cout << "Enter the maximum length of word to consider: ";
std::cin >> max_word;
// Get order of output
std::cout << "Enter 'a' for alphabetical order or 'n' for number of words order: ";
std::cin >> order;
}
// Write to file
template <typename T>
void fileOutput(const T& sorted_words)
{
std::ofstream out_file("output.txt");
if constexpr(std::is_same<T, std::multimap<int64_t, std::string_view>>::value)
{
for (std::multimap<int64_t, std::string_view>::const_reverse_iterator it = sorted_words.rbegin(); it != sorted_words.rend(); ++it)
{
out_file << it->first << '\t' << it->second << '\n';
}
}
else
{
for (const auto& it : sorted_words)
{
out_file << it.first << '\t' << it.second << '\n';
}
}
}
}