سلام؛ میتونید یک Meta data برای فایلتون در نظر بگیرید و اوّل فایل آدرس تمامی اون فایلهایی که ادغام شدند با طولشون رو بنویسید و بعد وقتی که خواستید دوباره اون فایلها رو باز کنید بیاید اوّل اون Meta data رو بخونید و بعد فایلها رو باز کنید.
یک مثال خیلی ساده:
#include <filesystem>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using std_file_path = std::filesystem::path;
using vec_path = std::vector<std_file_path>;
void append_data_to_file(const std_file_path& source, std::ofstream& f_dest)
{
std::ifstream f_source{source};
for (std::string line; std::getline(f_source, line);)
{
f_dest << line << '\n';
}
}
void write_meta_data_to_file(const std_file_path& file, std::ofstream& f_dest)
{
const auto file_name{std::filesystem::absolute(file)};
f_dest << file_name.string() << std::endl;
}
void write_all_in_one(const vec_path& file_path, const std_file_path& path)
{
std::ofstream out_file{path};
std::for_each(file_path.begin(), file_path.end(), [&](const auto i) {
write_meta_data_to_file(i, out_file);
});
out_file << "-----------E-META-DATA-------------\n";
std::for_each(file_path.begin(), file_path.end(), [&](const auto i) {
append_data_to_file(i, out_file);
});
}
void read_all_again(const std_file_path& p)
{
using namespace std::filesystem;
const auto out_path{current_path().string() + "/build"};
if (!exists(out_path))
{
create_directory(out_path);
}
vec_path data;
std::ifstream file{p};
std::string path_name;
while (file >> path_name)
{
data.push_back(path_name);
}
std::for_each(data.begin(), data.end(), [&](const auto i) {
if (exists(i.string()))
{
std::clog << "---------------------------------------" << std::endl;
std::clog << "file: " << i.string() << " exists." << std::endl;
std::clog << "copy: " << i << " to " << out_path << " ." << std::endl;
copy(i, out_path);
}
});
}
vec_path create_random_files()
{
vec_path data;
if (!std::filesystem::exists("./files"))
{
std::filesystem::create_directory("./files");
}
for (auto i{0}; i < 5; ++i)
{
std::string path{"./files/f" + std::to_string(i)};
std::ofstream file(path);
file << "ghasem" << std::to_string(std::hash<std::string>{}(path)) << " " << std::to_string(i);
file << std::endl;
data.push_back(path);
}
return data;
}
int main()
{
vec_path data{create_random_files()};
write_all_in_one(data, "out.text");
read_all_again("out.text");
}