Skip to content

Instantly share code, notes, and snippets.

@raspiduino
Last active July 7, 2024 16:08
Show Gist options
  • Save raspiduino/1eac926863463f68a7c5c841be2d06f3 to your computer and use it in GitHub Desktop.
Save raspiduino/1eac926863463f68a7c5c841be2d06f3 to your computer and use it in GitHub Desktop.
Alternative to parent_path() on std::filesystem::path with Windows style path
#include <filesystem>
#include <string>
#include <iostream>
#include <regex>
namespace fs = std::filesystem;
int main() {
fs::path p = "e:\\abc\\xyz.txt";
std::string s = p.string();
s = std::regex_replace(s, std::regex("\\\\"), "/");
std::string s2 = fs::path(s).remove_filename().string();
s2 = std::regex_replace(s2, std::regex("/"), "\\");
fs::path n = s2;
std::cout << "converted " << p << " to " << s << " then to " << n << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment