0

I am trying to parse input file (containing a text document with multiple lines and delimiters, i.e. "!,.?") into words. My function 'splitting function' is:

int splitInput(fp) {

    int i= 0;
    char  line[255];
    char *array[5000];
    int x;
    while (fgets(line, sizeof(line), fp) != NULL) {     
        array[i] = strtok(line, ",.!? \n");
        printf("Check print - word %i:%s:\n",i, array[i]);
        i++;
    }
    return 0;
}
gopi
  • 24
  • 1
  • 9
  • 4
    And? What is the problem? What is the expected output? What do you actually get? – LBes Nov 04 '15 at 21:44
  • Read the man page for strtok: http://www.cplusplus.com/reference/cstring/strtok/ – 001 Nov 04 '15 at 21:46
  • @gopi: How is strtok supposed to know which string to split, when you only send it NULL? You have to call strtok first with a pointer to the string you wish to split. Then you call it repeatedly with NULL, to get more tokens. – Thomas Padron-McCarthy Nov 04 '15 at 21:46
  • Yes, sorry - I've just realized it. It still doesn't work - even with 'line' replacing NULL. – gopi Nov 04 '15 at 21:48
  • 2
    _It still doesn't work_ Be specific. Is anything printing? Also, you need to call `strok` repeatedly until it returns `NULL` (first time with `line` as 1st parameter then with `NULL` as parameter. – 001 Nov 04 '15 at 21:56
  • @gopi: You need a loop for strtok, but not one that reads a completely new string from the user in each iteration. – Thomas Padron-McCarthy Nov 04 '15 at 21:58

2 Answers2

1

Here's the corrected function [sorry for extra the style cleanup]:

int
splitInput(fp)
{
    int i = 0;
    char *cp;
    char *bp;
    char line[255];
    char *array[5000];
    int x;

    while (fgets(line, sizeof(line), fp) != NULL) {
        bp = line;
        while (1) {
            cp = strtok(bp, ",.!? \n");
            bp = NULL;

            if (cp == NULL)
                break;
            array[i++] = cp;

            printf("Check print - word %i:%s:\n",i-1, cp);
        }
    }

    return 0;
}

Now, take a look at the man page for strtok to understand the bp trick

Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • @gopi You're welcome! I never give just "well you should check manpage"--pedantic/frustrating/useless for someone who has already made an honest effort like you. One way to teach is to give a hint or two: "you need an inner loop and look at strtok first arg closely". Better. But, I think people learn more with a complete solution [and easier for me--this comment is taking longer than the code :-)]. And, that's how I learned: reading others' code and asking "Why did they do _that_?". Anyway, happy programming!!! – Craig Estey Nov 05 '15 at 00:32
  • Exactly. I was simply stuck & needed a push. Thanks a lot - much appreciated! – gopi Nov 05 '15 at 21:23
0

If I understand your question correctly you want to read every line and split each line into words and add that into an array.

    array[i] = strtok(line, ",.!? \n");

That will not work for obvious reasons because it will only return the first word for each line and you never allocate memory.

This is probably what you want.

    char *pch;
    pch = strtok(line, ",.!? \n");
    while(pch != NULL) {
      array[i++] = strdup(pch); // put the content of pch into array at position i and increment i afterwards.
      pch = strtok(NULL, ",.!? \n"); // look for remaining words at the same line
    }

Don't forget to free your array elements afterwards though using free.

Linus
  • 1,516
  • 17
  • 35
  • @gopi great, don't forget to remove your increment of `i` after the `printf` because that will be redundant. – Linus Nov 04 '15 at 22:10