从C ++中的文件获取父目录

时间:2022-06-23 07:08:49

I need to get parent directory from file in C++:

我需要从C ++中的文件中获取父目录:

For example:

Input:

D:\Devs\Test\sprite.png

Output:

D:\Devs\Test\ [or D:\Devs\Test]

I can do this with a function:

我可以用一个函数做到这一点:

char *str = "D:\\Devs\\Test\\sprite.png";
for(int i = strlen(str) - 1; i>0; --i)
{
    if( str[i] == '\\' )
    {
        str[i] = '\0';
        break;
    }
}

But, I just want to know there is exist a built-in function. I use VC++ 2003.

但是,我只是想知道存在一个内置函数。我使用VC ++ 2003。

Thanks in advance.

提前致谢。

6 个解决方案

#1


1  

Editing a const string is undefined behavior, so declare something like below:

编辑const字符串是未定义的行为,因此声明如下所示:

char str[] = "D:\\Devs\\Test\\sprite.png";

You can use below 1 liner to get your desired result:

您可以使用1个以下的衬垫来获得所需的结果:

*(strrchr(str, '\\') + 1) = 0; // put extra NULL check before if path can have 0 '\' also

#2


12  

If you're using std::string instead of a C-style char array, you can use string::find_last_of and string::substr in the following manner:

如果您使用的是std :: string而不是C样式的char数组,则可以按以下方式使用string :: find_last_of和string :: substr:

std::string str = "D:\\Devs\\Test\\sprite.png";
str = str.substr(0, str.find_last_of("/\\"));

#3


4  

Heavy duty and cross platform way would be to use boost::filesystem::parent_path(). But obviously this adds overhead you may not desire.

重型和跨平台的方式是使用boost :: filesystem :: parent_path()。但显然这增加了你可能不想要的开销。

Alternatively you could make use of cstring's strrchr function something like this:

或者你可以使用cstring的strrchr函数,如下所示:

include <cstring>
char * lastSlash = strrchr( str, '\\');
if ( *lastSlash != '\n') *(lastSlash +1) = '\n';

#4


1  

On POSIX-compliant systems (*nix) there is a commonly available function for this dirname(3). On windows there is _splitpath.

在POSIX兼容系统(* nix)上,此目录有一个常用的函数(3)。在窗户上有_splitpath。

The _splitpath function breaks a path into its four components.

_splitpath函数将路径分成四个组件。

void _splitpath(
   const char *path,
   char *drive,
   char *dir,
   char *fname,
   char *ext 
);

So the result (it's what I think you are looking for) would be in dir.

所以结果(这是我认为你正在寻找的)将在dir中。

Here's an example:

这是一个例子:

int main()
{
    char *path = "c:\\that\\rainy\\day";
    char dir[256];
    char drive[8];
    errno_t rc;


    rc = _splitpath_s(
        path,       /* the path */
        drive,      /* drive */
        8,          /* drive buffer size */
        dir,        /* dir buffer */
        256,        /* dir buffer size */
        NULL,       /* filename */
        0,          /* filename size */
        NULL,       /* extension */
        0           /* extension size */
    );

    if (rc != 0) {
        cerr << GetLastError();
        exit (EXIT_FAILURE);
    }

    cout << drive << dir << endl;
    return EXIT_SUCCESS;
}

#5


0  

On Windows platforms, you can use PathRemoveFileSpec or PathCchRemoveFileSpec to achieve this. However for portability I'd go with the other approaches that are suggested here.

在Windows平台上,您可以使用PathRemoveFileSpec或PathCchRemoveFileSpec来实现此目的。但是为了便于携带,我会选择其他方法。

#6


-1  

You can use dirname to get the parent directory Check this link for more info

您可以使用dirname获取父目录检查此链接以获取更多信息

Raghu

#1


1  

Editing a const string is undefined behavior, so declare something like below:

编辑const字符串是未定义的行为,因此声明如下所示:

char str[] = "D:\\Devs\\Test\\sprite.png";

You can use below 1 liner to get your desired result:

您可以使用1个以下的衬垫来获得所需的结果:

*(strrchr(str, '\\') + 1) = 0; // put extra NULL check before if path can have 0 '\' also

#2


12  

If you're using std::string instead of a C-style char array, you can use string::find_last_of and string::substr in the following manner:

如果您使用的是std :: string而不是C样式的char数组,则可以按以下方式使用string :: find_last_of和string :: substr:

std::string str = "D:\\Devs\\Test\\sprite.png";
str = str.substr(0, str.find_last_of("/\\"));

#3


4  

Heavy duty and cross platform way would be to use boost::filesystem::parent_path(). But obviously this adds overhead you may not desire.

重型和跨平台的方式是使用boost :: filesystem :: parent_path()。但显然这增加了你可能不想要的开销。

Alternatively you could make use of cstring's strrchr function something like this:

或者你可以使用cstring的strrchr函数,如下所示:

include <cstring>
char * lastSlash = strrchr( str, '\\');
if ( *lastSlash != '\n') *(lastSlash +1) = '\n';

#4


1  

On POSIX-compliant systems (*nix) there is a commonly available function for this dirname(3). On windows there is _splitpath.

在POSIX兼容系统(* nix)上,此目录有一个常用的函数(3)。在窗户上有_splitpath。

The _splitpath function breaks a path into its four components.

_splitpath函数将路径分成四个组件。

void _splitpath(
   const char *path,
   char *drive,
   char *dir,
   char *fname,
   char *ext 
);

So the result (it's what I think you are looking for) would be in dir.

所以结果(这是我认为你正在寻找的)将在dir中。

Here's an example:

这是一个例子:

int main()
{
    char *path = "c:\\that\\rainy\\day";
    char dir[256];
    char drive[8];
    errno_t rc;


    rc = _splitpath_s(
        path,       /* the path */
        drive,      /* drive */
        8,          /* drive buffer size */
        dir,        /* dir buffer */
        256,        /* dir buffer size */
        NULL,       /* filename */
        0,          /* filename size */
        NULL,       /* extension */
        0           /* extension size */
    );

    if (rc != 0) {
        cerr << GetLastError();
        exit (EXIT_FAILURE);
    }

    cout << drive << dir << endl;
    return EXIT_SUCCESS;
}

#5


0  

On Windows platforms, you can use PathRemoveFileSpec or PathCchRemoveFileSpec to achieve this. However for portability I'd go with the other approaches that are suggested here.

在Windows平台上,您可以使用PathRemoveFileSpec或PathCchRemoveFileSpec来实现此目的。但是为了便于携带,我会选择其他方法。

#6


-1  

You can use dirname to get the parent directory Check this link for more info

您可以使用dirname获取父目录检查此链接以获取更多信息

Raghu