我想使用 GetDiskFreeSpace()
函数,但我需要根路径名。给定任何文件名输入,我可以使用什么函数来查找其卷路径名?
我从命令行获取输入的函数:
string targetFile;
cin.sync();
cin.clear();
cout << "Enter target file " << endl;
getline(cin, targetFile);
//if statement error handler for targetFile
fstream file(targetFile.c_str());
if (!file)
{
cout << "File does not exist" << endl;
}
file.close();
最佳答案
您可以使用 PathStripToRoot
功能来做到这一点。
请注意,文档建议您使用更安全的 PathCchStripToRoot
以避免潜在的缓冲区溢出,但这仅适用于 Windows 8。
为了支持相对路径,使用PathIsRelative
来检测相对路径。如果路径是相对路径,则将工作目录传递给上述函数之一。
关于c++ - 如何查找根路径名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24128848/