Problem with strtok()

This is a discussion on Problem with strtok() within the C Programming forums, part of the General Programming Boards category; I was under the impression strtok was supposed to return NULL when it hits the end of a string. It's ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    161

    Problem with strtok()

    I was under the impression strtok was supposed to return NULL when it hits the end of a string. It's not doing it when it should here. I'm testing with a multiline edit control with only 1 line of text (no \n). It continues into the loop a second time even though it shouldn't. Any ideas?

    Code:
    GetWindowText(hCode, txtCode, sizeof(txtCode));
    ParseCodes(txtCode, strlen(txtCode)
    
    int ParseCodes(char* code, int length)
    {
        u32 address[CODE_MAX_LINES], value[CODE_MAX_LINES], codevalue;
        char type[CODE_MAX_LINES];
        int i = 0, c = 0;
        char *codeline, codepart[20];
        length = RemoveChar(code, length, ' ');
        codeline = strtok(code, "\n");
        CheatDB.System = SYSTEM_N64;
        while (codeline != NULL)
        {
            switch(CheatDB.System)
            {
                case SYSTEM_N64: case SYSTEM_PS1:
                {
                    if (strlen(codeline) != 12) { return 0; }
                    if (!isHex(codeline)) { return 0; }
                    strncpy(codepart,codeline, 2); codepart[2] = '\0';
                    String2Hex(codepart, &codevalue);
                    c++;
                } break;
            }
            codeline = strtok(NULL, "\n");
        }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And what is the value of codeline the second time through? (Or the first time, or code to begin with?)

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Doh. I figured out what I was doing wrong. I had that function filtering the spaces out. It ended the string with a \n instead of a \0. Why do I even bother.
    Last edited by Viper187; 10-20-2008 at 07:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  2. Replies: 6
    Last Post: 04-28-2006, 01:06 PM
  3. Strange problem - strtok
    By AngKar in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 08:36 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21