[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppGetPath
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppGetPath
(C++) How to get a file's path
A common std::string manipulation used in File I/O://Returns the path, ending with a backslash '\' std::string getPath(const std::string& fileName) { const int endOfPathIndex = fileName.rfind("\\",fileName.size()); assert(endOfPathIndex < static_cast<int>(fileName.size())); return fileName.substr(0,endOfPathIndex+1); } int main() { assert(getPath("test\\file.exe")=="test\\"); std::cin.get(); }
- include <string>
- include <assert>
Code links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
