Can you tell me how I can adjust the findLargestNumber() function such that it uses the output_path?
static int findLargestNumber()
{
int best = -1;
const std::string prefix = "pairs";
const std::string suffix = ".bin";
for (const auto &e : fs::directory_iterator(fs::current_path()))
{
if (!e.is_regular_file()) continue;
const std::string name = e.path().filename().string();
if (name.size() < prefix.size() + suffix.size()) continue;
if (name.substr(0, prefix.size()) != prefix) continue;
if (name.substr(name.size()-suffix.size()) != suffix) continue;
const std::string numberPart =
name.substr(prefix.size(),
name.size() - prefix.size() - suffix.size());
try {
int n = std::stoi(numberPart);
if (n > best) best = n;
} catch (...) {}
}
return best; // −1 if none exists
}
/* ================================================================
Main
================================================================ */
int main(int argc, char* argv[])
{
std::string output_path = "./"; // default output directory
// parse command line arguments
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "-f" && i + 1 < argc) {
output_path = argv[++i];
if (output_path.back() != '/') {
output_path += '/';
}
} else {
std::cerr << "Usage: " << argv[0] << " [-f <output_path>]\n";
return 1;
}
}
const uint8_t rule = 30; // ECA rule (0 … 255)
const size_t L = 256; // lattice length (multiple of 64)
const size_t T = 1; // # time steps (>= 1)
const size_t N = 5*1024*1024; // # (x,y) samples to generate
const size_t F = 10; // # files to write
if (rule > 255 || (L & 63u) || L == 0) {
std::cerr << "invalid parameters\n";
return 1;
}
// next free file name data<number>.bin
int file_number = findLargestNumber(output_path) + 1;
for (int i = 0; i < F; i++) {
const std::string file = output_path + "pairs" + std::to_string(file_number) + ".bin";
std::ofstream ofs(file, std::ios::binary | std::ios::app);
if (!ofs) {
std::cerr << "cannot open " << file << '\n';
return 1;
}
// allocate two buffers of words
Vec bufA(L / 64), bufB(L / 64);
Vec *cur = &bufA;
Vec *nxt = &bufB;
for (size_t sample = 0; sample < N; ++sample)
{
random_fill(*cur); // random x
write_vec(*cur, ofs); // write x
for (size_t t = 0; t < T; ++t) {
eca_step(rule, *cur, *nxt);
std::swap(cur, nxt);
}
write_vec(*cur, ofs); // write y = f^T(x)
}
std::cout << "wrote " << N << " samples to " << file << '\n';
file_number++;
}
return 0;
}