1

I have a variable in which I read a .txt file. The file looks like this:

Element1|Element2|Element3|...|ElementLast#Element1|Element2|Element3|...|ElementLast#Element1|Element2|Element3|...|ElementLast#Element1|Element2|Element3|...|ElementLast


Now what I wanna do is, to get every 'Element1' and check if it is equal to an specific string.
I read much about the function strtok() and I tried to use it.
This is my function: (id is this special string, I convert it during the function.)

int PLUexists(int id) {

    char idc[3];
    sprintf(idc, "%d", id);
    printf("%s", idc);

    /* My Main String */
    FILE *plu = fopen("kassa_plu.txt", "r");
    char pluc[2000];
    while( fgets(pluc, sizeof(pluc), plu) !=0 );

    /* My Token */
    char *token;

    /* get first line */
    token = strtok(pluc, "#");

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

        // Without that block, I get all lines seperated...
        char t[255];
        strcpy(t, token);

        if ( strcmp(strtok(t, "|"), idc) == 0) {
            printf("Jep");
            return 1;
        }
        // End 'block'

        token = strtok(NULL, "#");
    }
    return 0;
}

Now if I just print out the first token (and repeat it), I get all of my lines seperated (see underneath) but when I also parse the code between the printf(" %s \n", token); and token = strtok(NULL, "#"); it won't work.

Without the block:

Element1|Element2|Element3|...|ElementLast
Element1|Element2|Element3|...|ElementLast
Element1|Element2|Element3|...|ElementLast
Element1|Element2|Element3|...|ElementLast

With it:

Element1|Element2|Element3|...|ElementLast
Element2|Element3|...|ElementLast
Element3|...|ElementLast
...|ElementLast
ElementLast
Fabian
  • 100
  • 11
  • 3
    You could look up `strtok_r` which will allow you to have nested "strtok loops". – user253751 Jan 05 '17 at 21:50
  • 2
    `while( fgets(pluc, sizeof(pluc), plu) !=0 );` will rapidly reach EOF with that semicolon there, without doing anything. After removing it, does the following code block need `{ braces }` around it? – Weather Vane Jan 05 '17 at 21:52
  • why use strtok -- wouldn't strstr be fine? – Hogan Jan 05 '17 at 21:56
  • You're right about the loop, @WeatherVane, but perhaps the OP *wants* to process only the last line of his file. If so, and if the file can be relied upon not to be completely empty, then that's an ok way to do it. – John Bollinger Jan 05 '17 at 22:06
  • See [Using `strtok()` in a loop in C](https://stackoverflow.com/questions/1509654/using-strtok-in-a-loop-in-c) and [Why is `strtok()` considered unsafe?](https://stackoverflow.com/questions/5999418/why-is-strtok-considered-unsafe) – Jonathan Leffler Jan 05 '17 at 22:14

1 Answers1

1

Your main problem is that when, inside the loop, you call strtok() with a non-null first argument, you thereby tell that function to abandon the previous tokenization (by # delimiters) it was working on to perform a new tokenization. You could consider using strtok_r() instead, which allows you to maintain state for multiple tokenizations at once, but since you want the first token on each line and only the first, I'd consider not using strtok[_r]() in the inner loop at all.

For example, here's a similar approach based on strchr():

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

        char *delim = strchr(token, '|');
        if (delim) *delim = '\0';

        if (strcmp(token, idc) == 0) {
            printf("Jep");
            return 1;
        }

        token = strtok(NULL, "#");
    }
John Bollinger
  • 160,171
  • 8
  • 81
  • 157