I'll help you modify the findLargestNumber() function to use the output_path parameter. Here's the adjusted version:
static int findLargestNumber(const std::string& output_path)
{
int best = -1;
const std::string prefix = "pairs";
const std::string suffix = ".bin";
for (const auto &e : fs::directory_iterator(output_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
}
The main changes are:
- Added a parameter to the function:
const std::string& output_path
- Changed
fs::directory_iterator(fs::current_path()) to fs::directory_iterator(output_path)
Now the function will search for the largest number in the specified output directory instead of the current working directory. The rest of the logic remains the same - it still looks for files matching the pattern "pairs[number].bin" and returns the largest number found or -1 if no matching files exist.
Make sure to update the function declaration if it's declared elsewhere in your code, and verify that the output_path exists before calling the function to avoid any filesystem-related errors.