我在C++上有一个档案名称(C:pierfoo.txt
,而且我需要检索文件夹名称(C:cleer
)。 在C#中,我会这样做:
string folder = new FileInfo("C:folderfoo.txt").DirectoryName;
在C++中是否有从档案名称中提取道路的功能?
我在C++上有一个档案名称(C:pierfoo.txt
,而且我需要检索文件夹名称(C:cleer
)。 在C#中,我会这样做:
string folder = new FileInfo("C:folderfoo.txt").DirectoryName;
在C++中是否有从档案名称中提取道路的功能?
https://msdn.microsoft.com/en-us/library/bb773748.aspx”rel=“nofollow noreferer” PathRemoveFileSpec。 如果你只支持Windows 8和后来,则建议使用https://msdn.microsoft.com/en-us/library/h7092.aspx” rel=“nofollow noreferer” PathCchRemoveFileSpec. 除其他改进外,它不再局限于<代码>。 ±_PATH (260) natures.
• 利用Bosst.Filesystem:
boost::filesystem::path p("C:\folder\foo.txt");
boost::filesystem::path dir = p.parent_path();
Example from 。 http://www.cplus.com/vis/string/string/find_last_of/
// string::find_last_of
#include <iostream>
#include <string>
using namespace std;
void SplitFilename (const string& str)
{
size_t found;
cout << "Splitting: " << str << endl;
found=str.find_last_of("/\");
cout << " folder: " << str.substr(0,found) << endl;
cout << " file: " << str.substr(found+1) << endl;
}
int main ()
{
string str1 ("/usr/bin/man");
string str2 ("c:\windows\winhelp.exe");
SplitFilename (str1);
SplitFilename (str2);
return 0;
}
在C++17中,有一个类别:
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
for(fs::path p : {"/var/tmp/example.txt", "/", "/var/tmp/."})
std::cout << "The parent path of " << p
<< " is " << p.parent_path() <<
;
}
可能的产出:
The parent path of "/var/tmp/example.txt" is "/var/tmp"
The parent path of "/" is ""
The parent path of "/var/tmp/." is "/var/tmp"
为什么必须如此复杂?
#include <windows.h>
int main(int argc, char** argv) // argv[0] = C:dev est.exe
{
char *p = strrchr(argv[0], \ );
if(p) p[0] = 0;
printf(argv[0]); // argv[0] = C:dev
}
auto p = boost::filesystem::path("test/folder/file.txt");
std::cout << p.parent_path() <<
; // test/folder
std::cout << p.parent_path().filename() <<
; // folder
std::cout << p.filename() <<
; // file.txt
您可能需要p.oul_path().filename()
才能获得父母的双倍名。
使用动力:档案系统。 它将纳入下一个标准,以便你也能使用。
我感到非常惊讶的是,没有人提到波六州的标准方式。
请使用<条码>基名称/<>条码>。
男性
标准C++在这方面为你赢得了很多成绩,因为路标是平台特有的。 您可以人工授精(如低科医生的回答),使用操作系统设施(例如:,或可能是最佳办法,你可以使用第三方系统,如档案强化材料:
I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...
I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?