English 中文(简体)
从档案名称获取名录
原标题:Getting a directory name from a filename

我在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;
}

为什么必须如此复杂?

#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()才能获得父母的双倍名。

使用动力:档案系统。 它将纳入下一个标准,以便你也能使用。

我感到非常惊讶的是,没有人提到波六州的标准方式。

请使用<条码>基名称/<>条码>。

男性





相关问题
Undefined reference

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 ...

C++ Equivalent of Tidy

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 ...

Template Classes in C++ ... a required skill set?

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?

Print possible strings created from a Number

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->...

typedef ing STL wstring

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, ...

C# Marshal / Pinvoke CBitmap?

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 ...

Window iconification status via Xlib

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?

热门标签