我正在尝试从传入的文件名(例如
我有一些代码,但我的思维混乱,我无法理解它。我该如何重构/重写它?
C:\some\dir\file
)动态获取父目录(假设是C:\some\dir
),并将其放入char*
中。我已经有了完整的路径和文件名char*
。如何在C语言中实现这一点?我有一些代码,但我的思维混乱,我无法理解它。我该如何重构/重写它?
/* 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);
char *strrchr(const char *string, int c);
将strrchr(File, '\\') = '\0';
,注意strtok()
会修改第一个参数。 - Grijesh Chauhan