从C中的字符串获取子字符串

从C中的字符串获取子字符串,c,string,split,C,String,Split,我有这样的字符串(它实际上是一个表示系统路径的字符串): /home/user/dir1/dir2/ 现在,我希望能够创建呈现的目录树,所以我需要创建home、user、dir1、dir2 我知道如何在Linux中用C创建dir,但在切掉字符串时遇到了麻烦。基本上,我现在需要的是一个字符串数组: tmp[0] = "./home/"; tmp[1] = "./home/user/"; tmp[2] = "./home/user/dir1/"; tmp[3] = "./home/user/dir1

我有这样的字符串(它实际上是一个表示系统路径的字符串):

/home/user/dir1/dir2/

现在,我希望能够创建呈现的目录树,所以我需要创建home、user、dir1、dir2

我知道如何在Linux中用C创建dir,但在切掉字符串时遇到了麻烦。基本上,我现在需要的是一个字符串数组:

tmp[0] = "./home/";
tmp[1] = "./home/user/";
tmp[2] = "./home/user/dir1/";
tmp[3] = "./home/user/dir1/dir2/";

如果我有这样的数组,那么创建目录树就很容易了,但是如何用这种方式分割字符串呢?

这有点幼稚,但应该可以让你开始了。它将处理可能有或可能没有尾随
/
的路径以及转义路径,例如
/home/some\/user/dir1/dir2

#include <stdio.h>                                                                                                                  
#include <string.h>

int main(int argc, char *argv[])
{
    char path[] = "./home/user/dir1/dir2";
    char *start = path;
    char *end = start + strlen(path);

    while (start < end) {
        char *slash = strchr(start, '/');

        if (slash) {
            if (slash > path && *(slash - 1) == '\\') {
                start = slash + 1;
                continue;
            }

            *slash = 0;
        }

        if (strcmp(start, ".") != 0) {
            /* Use 'path' for mkdir here */
            /* mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) or whatever */
            printf("going to make %s\n", path);
        }

        if (!slash) {
            break;
        }

        *slash = '/';
        start = slash + 1;
    }

    return 0;
}
#包括
#包括
int main(int argc,char*argv[])
{
字符路径[]=“/home/user/dir1/dir2”;
char*start=path;
char*end=start+strlen(路径);
while(开始<结束){
char*slash=strchr(开始“/”);
如果(斜杠){
如果(斜杠>路径&&*(斜杠-1)='\\'){
开始=斜杠+1;
继续;
}
*斜杠=0;
}
如果(strcmp(开始,“.”!=0){
/*在此处为mkdir使用“路径”*/
/*mkdir(路径,S|u IRWXU | S|u IRWXG | S|u IROTH | S|IXOTH)或其他什么*/
printf(“将生成%s\n”,路径);
}
如果(!斜杠){
打破
}
*斜杠='/';
开始=斜杠+1;
}
返回0;
}

这有点幼稚,但应该让您开始学习。它将处理可能有或可能没有尾随
/
的路径以及转义路径,例如
/home/some\/user/dir1/dir2

#include <stdio.h>                                                                                                                  
#include <string.h>

int main(int argc, char *argv[])
{
    char path[] = "./home/user/dir1/dir2";
    char *start = path;
    char *end = start + strlen(path);

    while (start < end) {
        char *slash = strchr(start, '/');

        if (slash) {
            if (slash > path && *(slash - 1) == '\\') {
                start = slash + 1;
                continue;
            }

            *slash = 0;
        }

        if (strcmp(start, ".") != 0) {
            /* Use 'path' for mkdir here */
            /* mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) or whatever */
            printf("going to make %s\n", path);
        }

        if (!slash) {
            break;
        }

        *slash = '/';
        start = slash + 1;
    }

    return 0;
}
#包括
#包括
int main(int argc,char*argv[])
{
字符路径[]=“/home/user/dir1/dir2”;
char*start=path;
char*end=start+strlen(路径);
while(开始<结束){
char*slash=strchr(开始“/”);
如果(斜杠){
如果(斜杠>路径&&*(斜杠-1)='\\'){
开始=斜杠+1;
继续;
}
*斜杠=0;
}
如果(strcmp(开始,“.”!=0){
/*在此处为mkdir使用“路径”*/
/*mkdir(路径,S|u IRWXU | S|u IRWXG | S|u IROTH | S|IXOTH)或其他什么*/
printf(“将生成%s\n”,路径);
}
如果(!斜杠){
打破
}
*斜杠='/';
开始=斜杠+1;
}
返回0;
}

这有点幼稚,但应该让您开始学习。它将处理可能有或可能没有尾随
/
的路径以及转义路径,例如
/home/some\/user/dir1/dir2

#include <stdio.h>                                                                                                                  
#include <string.h>

int main(int argc, char *argv[])
{
    char path[] = "./home/user/dir1/dir2";
    char *start = path;
    char *end = start + strlen(path);

    while (start < end) {
        char *slash = strchr(start, '/');

        if (slash) {
            if (slash > path && *(slash - 1) == '\\') {
                start = slash + 1;
                continue;
            }

            *slash = 0;
        }

        if (strcmp(start, ".") != 0) {
            /* Use 'path' for mkdir here */
            /* mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) or whatever */
            printf("going to make %s\n", path);
        }

        if (!slash) {
            break;
        }

        *slash = '/';
        start = slash + 1;
    }

    return 0;
}
#包括
#包括
int main(int argc,char*argv[])
{
字符路径[]=“/home/user/dir1/dir2”;
char*start=path;
char*end=start+strlen(路径);
while(开始<结束){
char*slash=strchr(开始“/”);
如果(斜杠){
如果(斜杠>路径&&*(斜杠-1)='\\'){
开始=斜杠+1;
继续;
}
*斜杠=0;
}
如果(strcmp(开始,“.”!=0){
/*在此处为mkdir使用“路径”*/
/*mkdir(路径,S|u IRWXU | S|u IRWXG | S|u IROTH | S|IXOTH)或其他什么*/
printf(“将生成%s\n”,路径);
}
如果(!斜杠){
打破
}
*斜杠='/';
开始=斜杠+1;
}
返回0;
}

这有点幼稚,但应该让您开始学习。它将处理可能有或可能没有尾随
/
的路径以及转义路径,例如
/home/some\/user/dir1/dir2

#include <stdio.h>                                                                                                                  
#include <string.h>

int main(int argc, char *argv[])
{
    char path[] = "./home/user/dir1/dir2";
    char *start = path;
    char *end = start + strlen(path);

    while (start < end) {
        char *slash = strchr(start, '/');

        if (slash) {
            if (slash > path && *(slash - 1) == '\\') {
                start = slash + 1;
                continue;
            }

            *slash = 0;
        }

        if (strcmp(start, ".") != 0) {
            /* Use 'path' for mkdir here */
            /* mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) or whatever */
            printf("going to make %s\n", path);
        }

        if (!slash) {
            break;
        }

        *slash = '/';
        start = slash + 1;
    }

    return 0;
}
#包括
#包括
int main(int argc,char*argv[])
{
字符路径[]=“/home/user/dir1/dir2”;
char*start=path;
char*end=start+strlen(路径);
while(开始<结束){
char*slash=strchr(开始“/”);
如果(斜杠){
如果(斜杠>路径&&*(斜杠-1)='\\'){
开始=斜杠+1;
继续;
}
*斜杠=0;
}
如果(strcmp(开始,“.”!=0){
/*在此处为mkdir使用“路径”*/
/*mkdir(路径,S|u IRWXU | S|u IRWXG | S|u IROTH | S|IXOTH)或其他什么*/
printf(“将生成%s\n”,路径);
}
如果(!斜杠){
打破
}
*斜杠='/';
开始=斜杠+1;
}
返回0;
}

我会接受肖恩的建议,即“你应该执行exec()mkdir-p…省去你的头疼。”

但是,如果需要C,则可以:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUF_SIZE 100

int main(){
    char s[] = "./home/user/dir1/dir2/";
    char** tmp;
    int i, j;
    int size = 0;
    char* token;

    /* count the '/' characters */
    char c = s[0];
    int count = 0;
    i = 0;
    while (c != '\0'){
        if (s[i] == '/')
            count++;

        i++;
        c = s[i];
    }

    size = i;

    /* ready the tmp array */
    tmp = (char**)malloc(count);
    for (i = 0; i < count; i++){
        tmp[i] = (char*)malloc(BUF_SIZE);
        for (j = 0; j < BUF_SIZE; ++j)
            tmp[i][j] = '\0';
    }

    /* special treatment for our first tmp[] */
    tmp[0][0] = '.';
    tmp[0][1] = '/';


    i = 0;
    /* tokenize the main string */
    token = strtok(s, "./");
    while (token != NULL){

        if (i > 0)
            strcat(tmp[i], tmp[i - 1]);

        strcat(tmp[i], token);
        strcat(tmp[i], "/");
        printf("%s\n", tmp[i]);

        token = strtok(NULL, "/");
        i++;
    }

    /* clean up */
    for (i = 0; i < count; i++)
        free(tmp[i]);

    getchar();
    return 0;
}

我会接受肖恩的建议:“你应该执行exec()mkdir-p…省去你的头痛。”

但是,如果需要C,则可以:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUF_SIZE 100

int main(){
    char s[] = "./home/user/dir1/dir2/";
    char** tmp;
    int i, j;
    int size = 0;
    char* token;

    /* count the '/' characters */
    char c = s[0];
    int count = 0;
    i = 0;
    while (c != '\0'){
        if (s[i] == '/')
            count++;

        i++;
        c = s[i];
    }

    size = i;

    /* ready the tmp array */
    tmp = (char**)malloc(count);
    for (i = 0; i < count; i++){
        tmp[i] = (char*)malloc(BUF_SIZE);
        for (j = 0; j < BUF_SIZE; ++j)
            tmp[i][j] = '\0';
    }

    /* special treatment for our first tmp[] */
    tmp[0][0] = '.';
    tmp[0][1] = '/';


    i = 0;
    /* tokenize the main string */
    token = strtok(s, "./");
    while (token != NULL){

        if (i > 0)
            strcat(tmp[i], tmp[i - 1]);

        strcat(tmp[i], token);
        strcat(tmp[i], "/");
        printf("%s\n", tmp[i]);

        token = strtok(NULL, "/");
        i++;
    }

    /* clean up */
    for (i = 0; i < count; i++)
        free(tmp[i]);

    getchar();
    return 0;
}

我会接受肖恩的建议:“你应该执行exec()mkdir-p…省去你的头痛。”

但是,如果需要C,则可以:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUF_SIZE 100

int main(){
    char s[] = "./home/user/dir1/dir2/";
    char** tmp;
    int i, j;
    int size = 0;
    char* token;

    /* count the '/' characters */
    char c = s[0];
    int count = 0;
    i = 0;
    while (c != '\0'){
        if (s[i] == '/')
            count++;

        i++;
        c = s[i];
    }

    size = i;

    /* ready the tmp array */
    tmp = (char**)malloc(count);
    for (i = 0; i < count; i++){
        tmp[i] = (char*)malloc(BUF_SIZE);
        for (j = 0; j < BUF_SIZE; ++j)
            tmp[i][j] = '\0';
    }

    /* special treatment for our first tmp[] */
    tmp[0][0] = '.';
    tmp[0][1] = '/';


    i = 0;
    /* tokenize the main string */
    token = strtok(s, "./");
    while (token != NULL){

        if (i > 0)
            strcat(tmp[i], tmp[i - 1]);

        strcat(tmp[i], token);
        strcat(tmp[i], "/");
        printf("%s\n", tmp[i]);

        token = strtok(NULL, "/");
        i++;
    }

    /* clean up */
    for (i = 0; i < count; i++)
        free(tmp[i]);

    getchar();
    return 0;
}

我会接受肖恩的建议:“你应该执行exec()mkdir-p…省去你的头痛。”

但是,如果需要C,则可以:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUF_SIZE 100

int main(){
    char s[] = "./home/user/dir1/dir2/";
    char** tmp;
    int i, j;
    int size = 0;
    char* token;

    /* count the '/' characters */
    char c = s[0];
    int count = 0;
    i = 0;
    while (c != '\0'){
        if (s[i] == '/')
            count++;

        i++;
        c = s[i];
    }

    size = i;

    /* ready the tmp array */
    tmp = (char**)malloc(count);
    for (i = 0; i < count; i++){
        tmp[i] = (char*)malloc(BUF_SIZE);
        for (j = 0; j < BUF_SIZE; ++j)
            tmp[i][j] = '\0';
    }

    /* special treatment for our first tmp[] */
    tmp[0][0] = '.';
    tmp[0][1] = '/';


    i = 0;
    /* tokenize the main string */
    token = strtok(s, "./");
    while (token != NULL){

        if (i > 0)
            strcat(tmp[i], tmp[i - 1]);

        strcat(tmp[i], token);
        strcat(tmp[i], "/");
        printf("%s\n", tmp[i]);

        token = strtok(NULL, "/");
        i++;
    }

    /* clean up */
    for (i = 0; i < count; i++)
        free(tmp[i]);

    getchar();
    return 0;
}

我将使用
strtok
解析字符串中的目录名,使用
“/”
作为分隔符

见:

我是这样做的:

#include <stdio.h>
#include <string.h>

// Count the number of times the character appears in the string
size_t countInstances(char* str, char token) {
    size_t count = 0;
    while(*str) {
        if(*str == token) {
            count++;
        }
        str++;
    }

    return count;
}

int main() {
    char path[] = "./home/user/dir1/dir2/"; // strtok might segfault if this is a pointer (rather than an array)

    size_t count = countInstances(path, '/');
    char** dirs = malloc(sizeof(*dirs) * count);
    char* dir;
    size_t i = 0;

    dir = strtok(path, "/");
    while(dir && i < count) {
        dirs[i] = dir; // store reference

        printf("%s\n",dir);
        dir = strtok (NULL, "/");
        i++;
    }

    free(dirs);
    return 0;
}

我将使用
strtok
解析字符串中的目录名,使用
“/”
作为分隔符

见:

我是这样做的:

#include <stdio.h>
#include <string.h>

// Count the number of times the character appears in the string
size_t countInstances(char* str, char token) {
    size_t count = 0;
    while(*str) {
        if(*str == token) {
            count++;
        }
        str++;
    }

    return count;
}

int main() {
    char path[] = "./home/user/dir1/dir2/"; // strtok might segfault if this is a pointer (rather than an array)

    size_t count = countInstances(path, '/');
    char** dirs = malloc(sizeof(*dirs) * count);
    char* dir;
    size_t i = 0;

    dir = strtok(path, "/");
    while(dir && i < count) {
        dirs[i] = dir; // store reference

        printf("%s\n",dir);
        dir = strtok (NULL, "/");
        i++;
    }

    free(dirs);
    return 0;
}

我将使用
strtok
解析字符串中的目录名,使用
“/”
作为分隔符

见:

H