如何将目录拼接到包含C中路径名的char*中?

如何将目录拼接到包含C中路径名的char*中?,c,string,unix,char,C,String,Unix,Char,所以现在我正试图将目录名拼接到路径名的中间 例如,假设我想在路径中OTHERDIRNAME出现的位置之后拼接DIRNAME。例如,假设路径为: /home/user/folder/OTHERDIRNAME/morefolders/test/etc 我的目标是获得如下所示的新路径名: /home/user/folder/OTHERDIRNAME/DIRNAME/morefolders/test/etc 顺便说一下,我有用于保存旧路径名的变量,以及我希望将新目录拼接到其中的目录名。所以我只需要帮

所以现在我正试图将目录名拼接到路径名的中间

例如,假设我想在路径中OTHERDIRNAME出现的位置之后拼接DIRNAME。例如,假设路径为:

/home/user/folder/OTHERDIRNAME/morefolders/test/etc
我的目标是获得如下所示的新路径名:

/home/user/folder/OTHERDIRNAME/DIRNAME/morefolders/test/etc

顺便说一下,我有用于保存旧路径名的变量,以及我希望将新目录拼接到其中的目录名。所以我只需要帮助在C中使用str函数来尝试在正确的位置拼接DIRNAME。我尝试过使用strtok,但使用OTHERDIRNAME作为delimeter时似乎遇到了问题,因为我认为delimeter参数必须是单个字符…

这非常简单,但可能会让人困惑。使用
strstr
在源字符串中搜索“分隔符”。添加分隔符的长度,使我们指向拼接的位置。然后调用3个适当长度的
memcpy

#include <stdio.h>
#include <string.h>
int main()
{
    char str[128] = "/home/user/folder/OTHERDIRNAME/morefolders/test/etc";
    char* delim = "/";
    char* tok;
    char buf[128];

    tok = strtok(str,delim);
    strcpy(buf,"/");
    while(tok)
    {
            strcat(buf,tok);
            strcat(buf,"/");
            if(strcmp(tok,"OTHERDIRNAME") == 0)
            {
                    strcat(buf,"DIRNAME");
                    strcat(buf,"/");
            }

            tok = strtok(NULL,delim);
    }

    printf("Dir path: %s\n", buf);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char *dir = "/home/user/folder/OTHERDIRNAME/morefolders/test/etc";
    char *seek = "/OTHERDIRNAME/";
    char *ins = "DIRNAME/";
    char *splice_point;
    char *result;

    splice_point = strstr(dir, seek); // points to the first slash we're interested in
    splice_point += strlen(seek);     // now points to the second slash
    result = malloc(strlen(dir)+strlen(ins)+1);  // allocate string of appropriate length
    memcpy(result, dir, splice_point - dir);     // copy the head
    memcpy(result + (splice_point - dir), ins, strlen(ins));  // copy the splice
    strcpy(result + (splice_point - dir) + strlen(ins), splice_point);  // copy the tail (and term)

    printf("%s\n", result);
}
#包括
#包括
#包括
int main(){
char*dir=“/home/user/folder/OTHERDIRNAME/morefolders/test/etc”;
char*seek=“/OTHERDIRNAME/”;
char*ins=“DIRNAME/”;
字符*拼接点;
字符*结果;
splice_point=strstrstr(dir,seek);//指向我们感兴趣的第一条斜线
拼接点+=strlen(seek);//现在指向第二个斜杠
result=malloc(strlen(dir)+strlen(ins)+1);//分配适当长度的字符串
memcpy(result,dir,splice_point-dir);//复制头部
memcpy(结果+(拼接点-dir)、ins、strlen(ins));//复制拼接
strcpy(结果+(拼接点-dir)+strlen(ins),拼接点);//复制尾部(和术语)
printf(“%s\n”,结果);
}

没错,strtok将匹配第二个参数中的任何单个字符作为分隔符

你应该做你想做的

从我的头顶(未编译和未测试):


当然,根据您的需要调整内存管理。如果保证缓冲区足够大,您可以就地执行该操作。

分隔符参数是一个字符串,但是
strtok
将只使用其中的一个字符(它找到的第一个字符),因此它被称为分隔符集。我使用
strtok
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char *dir = "/home/user/folder/OTHERDIRNAME/morefolders/test/etc";
    char *seek = "/OTHERDIRNAME/";
    char *ins = "DIRNAME/";
    char *splice_point;
    char *result;

    splice_point = strstr(dir, seek); // points to the first slash we're interested in
    splice_point += strlen(seek);     // now points to the second slash
    result = malloc(strlen(dir)+strlen(ins)+1);  // allocate string of appropriate length
    memcpy(result, dir, splice_point - dir);     // copy the head
    memcpy(result + (splice_point - dir), ins, strlen(ins));  // copy the splice
    strcpy(result + (splice_point - dir) + strlen(ins), splice_point);  // copy the tail (and term)

    printf("%s\n", result);
}
// Returns a malloc'd null-terminated string or NULL
char * replaceInString(const char * original, const char * match, const char * replace)
{
    const char * foundMatch = strstr(original, match);
    if (foundMatch)
    {
        ptrdiff_t offset = foundMatch - original;
        int matchLength = strlen(match);
        int replaceLength = strlen(replace);

        char * newString = malloc(strlen(original) + replaceLength - matchLength + 1);
        strncpy(newString, original, offset);
        strcpy(newString + offset, replace);
        strcpy(newString + offset + replaceLength, original + offset + matchLength);

        return newString;
    }
    return NULL;
}

// snip
   char * newDirName = replaceInString(oldDirName, "/OTHERDIRNAME/", "/OTHERDIRNAME/DIRNAME/");
// snip