-2

I got some troubles while I'm trying to set a char into a char* ("string")

I got lines, which is all the txt lines I fetched before, and now I'm trying to assign a char to my char*, to filter it.

Here is my actual code :

void TreatDatas(char** lignes, int sizeLignes){ // all the lines, and the size of it.
    char** finalArray;
    finalArray = malloc(2048 * sizeof(char*));
    int sizeOfFinalArray = 0;

    int i;
    int j;

    char* s;
    char* savedCurrentString = "";
    int sizeOfCurrentString = 0;

    for (i = 0; i< sizeLignes; i++){
        s = lignes[i];
        for (j = 0; j < strlen(s); j++){ // I don't pass the first condition the first loop
            if (s[j] == ',' || s[j] == '.' || s[j] == ' ' || s[j] == '\n' || s[j] == ';' || s[j] == ':'){ // Separators list
                finalArray[sizeOfFinalArray] =  malloc(strlen(savedCurrentString) + 1);
                strcpy(finalArray[sizeOfFinalArray], savedCurrentString);
                savedCurrentString = "";
                sizeOfCurrentString = 0;
            }else{

                printf("%c , %s \n", s[j], savedCurrentString); // L - ""
                printf("%d", sizeOfCurrentString); // 0
                strncpy(savedCurrentString, s[j], 1); // error here

            }
        }
    }
    free(finalArray);
}
Izio
  • 378
  • 6
  • 15
  • Never use `strncpy`. Also, you're trying to modify a string literal. – melpomene Sep 17 '17 at 12:59
  • `strncpy(savedCurrentString, s[j], 1);` : `savedCurrentString` is `""`, type of `s[j]` is `char`, not `char*`. – BLUEPIXY Sep 17 '17 at 13:00
  • post example of input and expected output – Andreas Sep 17 '17 at 13:12
  • Oh so what to use in my case? – Izio Sep 17 '17 at 13:13
  • @Andreas intput : "Lorem Ipsum", for the first line. Expected Output : "Lorem", "Ipsum" – Izio Sep 17 '17 at 13:14
  • making your first tokenizer eh? Those are often hard to overlook if implemented as a single function. For starters make functions "isDelimiter(character)" and "tokenize(string, head, tail)". Do tokenize until tail is empty. – Andreas Sep 17 '17 at 13:27
  • Ahah yes, didn't even know this is called tokenizer, thanks for the orientation to take. :) – Izio Sep 17 '17 at 13:32

1 Answers1

0

Ok I change a bit the code, now it's better, but seems to duplicate some elements. And I don't know why. Excuse me, I'm a C beginer, but I'm curious and trying to understand how that works.

void TreatDatas(char** lignes, int sizeLignes){

    char** finalArray;
    finalArray = malloc(2048 * sizeof(char*));
    int sizeOfFinalArray = 0;

    int i;
    int j;

    char* s;
    char* savedCurrentString = "";
    int sizeOfCurrentString = 0;

    for (i = 0; i< sizeLignes; i++){
        s = lignes[i];
        printf("line : %d \n %s \n", i, s);
        StringSpliter(s);
//        for (j = 0; j < strlen(s); j++){
//            finalArray[sizeOfFinalArray] =  malloc(strlen(savedCurrentString) + 1);
//        }
    }
    free(finalArray);
}

char * StringSpliter(char* input){
    char separators[5][2] = {" ", ",", ";", ":", "."};

    char ** buffer;
    int bufferSize = 0;

    int i;

    for (i = 0; i < 5; i++){

        char* token = strtok(input, separators[i]);
        printf("%s", token);

        while( token != NULL )
        {
            printf( " %s\n", token );

            token = strtok(NULL, separators[i]);
        }

    }

    printf("\n");

    return "OSEF";
}

My output

Izio
  • 378
  • 6
  • 15
  • This should probably be an update to the question, or perhaps a new question, since it isn't a complete working answer. Don't include links to unreadable pictures of the text output. Show the output in the question as plain text. (Treat it like code. For bonus points, add `` on a line on its own before the output — include a blank line before and after ii.) Read up on how to create an MCVE ([MCVE]) if you think the output is too long — what I saw of the picture suggests it is. – Jonathan Leffler Sep 17 '17 at 21:15