0

I am reading a separate .txt file and grabbing each line. Then I am using strtok to manipulate each string. I have a #define macro:

#define DELIMS "!\"#$%&()|'*+,?/:;<=>@[\092]^_{}~\177\t\ "

In the DELIMS I have a space at the very end. When I use this to split up a string, using this in the strtok, it isn't using the space as one of the delims. Am I doing something wrong?

void splitLine(char string[], int count)
{
    char *ptr, *word;

  for (ptr = string; word = strtok(ptr, DELIMS); ptr = NULL)
    printf("%s", word);
}

OUTPUT

define FLUSH while

It is displaying the three words in one printf instead of displaying each word one at a time.

Bryan
  • 117
  • 3
  • 13
  • `for (word = string; word = strtok(word, DELIMS); word = NULL)`, I see no need for `ptr` or `count`. Though trying to shove all this in the for-sections is a bit over the top don't you think? Certainly isn't adding clarity to the solution. Now how about some sample data, or is it the same [as yesterday](https://stackoverflow.com/questions/35960944/strtok-and-string-manipulation-issues) ? – WhozCraig Mar 14 '16 at 05:14
  • @WhozCraig yes it is the same from yesterday. I am still having issues and thought reposting might get some new input – Bryan Mar 14 '16 at 05:20
  • 1
    Ok, well, you don't have to escape a space in that delim string. I'm surprised your compiler didn't warn you. – WhozCraig Mar 14 '16 at 05:24
  • 2
    that \ you have right before the space at the end of your DELIMS string. it isn't needed. – WhozCraig Mar 14 '16 at 05:25
  • I removed it and it's still printing the three words at once instead of one at a time.... – Bryan Mar 14 '16 at 05:26
  • 1
    What do you think \092 is? – n. m. could be an AI Mar 14 '16 at 05:33
  • 1
    You may want to reconsider that and use \\ instead. As it sits now you're prolly unintentionally burying a nullchar \0 in your delimiter list (or so it appears to me). Just for kicks, move the space to the *beginning* of your delim string and try it then. – WhozCraig Mar 14 '16 at 05:39
  • 1
    I recommend you approach your sources of information critically. The page you have linked may or may not use notation which is compatible with C. Read some C documentation about string literals. If all else fails, try printing `strlen('\092')`. – n. m. could be an AI Mar 14 '16 at 05:40
  • So this shows that the documentation for "activePDF" is not a good source of C language information. Not that I understand why anyone would think that in the first place. – Michael Burr Mar 14 '16 at 06:18

0 Answers0