简体   繁体   English

字符串操作库

[英]String manipulation library

EDIT: Have included only the relevant code 编辑:只包括相关的代码

I have to manipulate an input string that looks like this 我必须操纵看起来像这样的输入字符串

create /foo
create /foo/bar
write /foo/bar "text"
create /foo/bar/baz

And I've created this program (you don't need to look at all of it) and the problem I have is with the function printAllFolders() which is called in the main() and it is defined right under the main() function. 我已经创造了这个程序(你不需要看这一切),我的问题是与功能printAllFolders()被称为在main() ,它的正下方定义main()功能。 The problem must be in that function. 问题必须出在那个功能上。 Is it correct to pass the string in the struct path[] giving the parameter comando->path ? 将字符串传递到给出参数comando->path的struct path[]是否正确?

When I put that function in the main it does give me segmentation fault issue. 当我把那个函数放到主体上时,确实给了我分割错误的问题。 The rest works just fine. 其余的一切都很好。

EDIT: just to be clear, the printAllFolders() does print all the strings in the path array, so I just need to pass the path[255] array, not the one with all two indexes. 编辑:只是要清楚, printAllFolders()确实打印了路径数组中的所有字符串,因此我只需要传递path[255]数组,而不是带有所有两个索引的数组。

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

typedef struct _command {
    unsigned char command[10];
    unsigned char path[255][255];
    int pathLevels;
} command;

command* createCommandMul(unsigned char* str);
void printfAllFolders(unsigned char* stringhe, int lengthArray);


int main() {
    command* comando = (command*) malloc(sizeof(command));
    unsigned char* upPath = NULL;
    unsigned char* allPath = NULL;
    FILE* fp;
    unsigned char* line = NULL;
    size_t len = 0;
    ssize_t read;

    fp = fopen("/Users/mattiarighetti/Downloads/semplice.txt", "r");
    if (fp == NULL)
        exit(EXIT_FAILURE);
    while ((read = getline(&line, &len, fp)) != -1) {
            comando = createCommandMul(line);
            upPath = upperPath(comando);
            allPath = fullPath(comando);
            printfAllFolders(comando->path, comando->pathLevels);
    }
    fclose(fp);
    if (line)
        free(line);
    return 0;
}

void printfAllFolders(unsigned char* stringhe, int lengthArray) {
    unsigned char* stringa = stringhe;
    int length = lengthArray;
    if (length == 0) printf("Cartella %s", stringa[length]);
    for (int i = 0; i < length+1; i++) {
        printf("Cartella %d %s\t", i, stringa[i]);
    }
}

command* createCommandMul(unsigned char* str) {
    unsigned char* c = str;
    command* commandPointer = (command*) malloc(sizeof(command));
    int commandIndex = 0;
    int pathLevel = 0;
    int pathIndex = 0;
    /* Parte Comando */
    while(*c != ' ' && commandIndex<10) {
        commandPointer->command[commandIndex] = *c++;
        commandIndex++;
    }
    commandPointer->command[commandIndex] = '\0';
    while(*c == ' ') c++;
    while(*c == '/') c++; 
    /* Parte Path*/
    while(*c!='\0') {
        if (*c == '/') {
            commandPointer->path[pathLevel][pathIndex] = '\0';
            pathLevel++;
            pathIndex = 0;
            c++;
        } else {
            commandPointer->path[pathLevel][pathIndex] = *c++;
            pathIndex++;
        }
    }
    commandPointer->path[pathLevel][pathIndex] = '\0';
    commandPointer->pathLevels = pathLevel;
    return commandPointer;
}

Perhaps there is some confusion about the difference between an array: 数组之间的区别也许有些困惑:

path[255][255]

and a pointer 和一个指针

unsigned char* stringhe

When passing the array -as- a pointer: 当将数组作为指针传递时:

printfAllFolders(comando->path, ...

the printfAllFolders() function sees stringhe as a memory address where there happens to be an unsigned char stored. printfAllFolders()函数将stringhe视为一个存储地址,恰好存储了一个未签名的char。 The printAllFolders() function does not know that stringhe actually points to a more complex object (array of unsigned char with [255][255] dimensions). printAllFolders()函数不知道string实际上指向一个更复杂的对象(尺寸为[255] [255]的无符号字符数组)。

One fix to the question code is to change: 问题代码的一种解决方法是更改​​:

    void printfAllFolders(unsigned char* stringhe, int lengthArray)

to the following: 到以下内容:

void printfAllFolders(unsigned char stringhe[255][255], int lengthArray)

This passes additional information to the function needed to understand the more complex nature of stringhe. 这会将附加信息传递给了解stringhe更复杂性质所需的功能。

Of course, the following line (again) removes this needed information: 当然,以下行(再次)删除了此必需的信息:

   unsigned char* stringa = stringhe;

Perhaps this line ^above^ should be eliminated? 也许应该删除“上面”这一行?

Then change the line: 然后更改行:

if (length == 0) printf("Cartella %s", stringa[length]);

to: 至:

if (length == 0) printf("Cartella %s", stringhe[length]);

and then change line: 然后换行:

        printf("Cartella %d %s\t", i, stringa[i]);

to: 至:

        printf("Cartella %d %s\t", i, stringhe[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM