CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2013
    Posts
    5

    token going null on second itteration of loop

    Code:
    while(!pile.eof()){
             pile.getline(inputbuff,199,'\n');
             count++;
          if(count % 2 != 0){
               tok = strtok(inputbuff,",");
                   tok = strtok (NULL,",");
               tok = strtok (NULL,",");
          }
          if(count % 2 == 0){
              CopyFile(inputbuff,ptr);
          }
        }
    the loop iterates the first time through qualifying for (count % 2 != 0)
    because count is at 1
    so it stores the characters after the comma into tok and does it successfully according to my debugger.
    then the while itterates again because it doesn't qualify for the next if.
    this time it does qualify for count % 2 == 0
    but my tok pointer is now null , how can that be? does it go null when the while loop itterates for the second time? that shouldn't be because my integer count doesn't reset? Please help

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: token going null on second itteration of loop

    First, some reformatting:
    Code:
    int count = 0;
    while(!pile.eof())
    {
         pile.getline(inputbuff,199,'\n');
         count++;
         if(count % 2 != 0)
         {
             tok = strtok(inputbuff,",");
             tok = strtok(NULL,",");
             tok = strtok (NULL,",");
         }
         if (count % 2 == 0)
              CopyFile(inputbuff,ptr);
    }
    Second, no one knows what's hidden behind that "inputbuff" character array. We don't know what the data is, therefore we can't answer your question. The tok is NULL because that is what it was assigned. Variables just don't change by magic.

    Unless you provide to us the actual data, or provide a full, but small program demonstrating the issue, then there is nothing wrong with what you're seeing.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: token going null on second itteration of loop

    I guess it's null because the second call to strtok (NULL,",") just returns NULL. Did you check that?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: token going null on second itteration of loop

    strtok returns a pointer to the next token found in the specified c-string. It returns NULL when no more tokens are found. Each call modifies the provided c-string by substituting a NULL character in the string for each delimiter that is encountered. As cilu said

    I guess it's null because the second call to strtok (NULL, ",") just returns NULL
    I suspect that the data read into inputbuffer is not always what you are expecting. You have no checking on the format of this and are assuming it is always right.

    What are you trying to accomplish?

    As Paul requested, please provide some actual data and a small complete program that demonstrates the problem.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: token going null on second itteration of loop

    I've just noticed that you are using ptr in CopyFile. Is that what you want or did you mean to use tok there?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured