[英]How to get the directory of a file from the full path in C
I'm trying to dynamically obtain the parent directory (let's say C:\some\dir
) from a file name I get in an argument (say C:\some\dir\file
), and put it in a char*
.我正在尝试从我在参数中获得的文件名(例如C:\some\dir\file
)动态获取父目录(例如C:\some\dir
dir ),并将其放入char*
中。 I already have the full path and file in a char*
.我已经在char*
中有完整的路径和文件。 How exactly would I do that in C?我将如何在 C 中做到这一点?
I have some code but in my mind it's all garbled and I can't make any sense of it.我有一些代码,但在我看来都是乱码,我无法理解。 How should I rework/rewrite this?我应该如何重做/重写这个?
/* Gets parent directory of file being compiled */
short SlashesAmount;
short NamePosition;
short NameLength;
char* Pieces[SlashesAmount];
char* SplitPath;
short ByteNumber;
short PieceNumber;
char* ScriptDir;
NameLength = strlen(File);
//Dirty work
SplitPath = strtok(File, "\");
do {
ByteNumber = 0;
do {
File[NamePosition] = CurrentPiece[ByteNumber];
NamePosition++;
} while(File[NamePosition] != '\n');
PieceNumber++;
} while(NamePosition < NameLength);
What you're looking for is dirname(3)
.您正在寻找的是dirname(3)
。 This is POSIX-only.这仅适用于 POSIX。
A Windows alternative would be _splitpath_s
. Windows 替代方案是_splitpath_s
。
errno_t _splitpath_s(
const char * path,
char * drive,
size_t driveNumberOfElements,
char * dir,
size_t dirNumberOfElements,
char * fname,
size_t nameNumberOfElements,
char * ext,
size_t extNumberOfElements
);
Sample code (untested):示例代码(未经测试):
#include <stdlib.h>
const char* path = "C:\\some\\dir\\file";
char dir[256];
_splitpath_s(path,
NULL, 0, // Don't need drive
dir, sizeof(dir), // Just the directory
NULL, 0, // Don't need filename
NULL, 0);
You already have the full path of the file (for example: C:\some\dir\file.txt), just:您已经有了文件的完整路径(例如:C:\some\dir\file.txt),只需:
1. find the last slash by strrchr() : called p 1. 通过 strrchr() 找到最后一个斜线:称为 p
2. copy from the beginning of the path to the p - 1 (do not include '/') 2.从路径的开头复制到p-1(不包括'/')
So the code will look like:所以代码看起来像:
char *lastSlash = NULL;
char *parent = NULL;
lastSlash = strrchr(File, '\\'); // you need escape character
parent = strndup(File, strlen(File) - (lastSlash - 1));
int len = strlen(filepath);
char* dir = malloc(len + 1);
strcpy(dir, filepath);
while (len > 0) {
len--;
if (dir[len] == '\\' || dir[len] == '/') {
dir[len] = '\0';
break;
}
}
Don't have enough reputation to comment so add a answer here.没有足够的声誉发表评论,因此请在此处添加答案。
As Grijesh Chauhan commented in question, you can use strrchr() to get a shorter version of your original string.正如 Grijesh Chauhan 所评论的那样,您可以使用 strrchr() 来获取原始字符串的较短版本。
However, the return value of strrchr()
is char *
, which SHOULD NOT be assigned to \0
that makes it points to nothing , instead, you can use *
or [0]
to modify it's first element to shorten the original string.但是, strrchr()
的返回值是char *
,不应将其分配给\0
使其指向 nothing ,相反,您可以使用*
或[0]
修改它的第一个元素以缩短原始字符串。
LIKE THIS:像这样:
strrchr(File, '\\')[0] = '\0'
// or this
*(strrchr(File, '\\') = '\0'
Great answer though, Grijesh should make it a answer :D很好的答案,Grijesh 应该让它成为一个答案:D
Here is a function that gets the full path and a buffer.这是一个获取完整路径和缓冲区的函数。 it will update the buffer to the parent path.它会将缓冲区更新为父路径。 The function is checked.检查功能。 enjoy :)请享用 :)
/*
Use: Get parent path by full path
Input: full path, parent buffer
Output: None
*/
void getParent(char* path, char* parent)
{
int parentLen;
char* last = strrchr(path, '/');
if (last != NULL) {
parentLen = strlen(path) - strlen(last + 1);
strncpy(parent, path, parentLen);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.