Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. strtok problem...

strtok problem...

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    u6ik
    wrote on last edited by
    #1

    Can anyone see a problem with this code? It keeps resetting the original string line so it jumps out of the loop after the first token. What it should do is parse the whole line... For some reason it doesn't seem to like the "\\" delimiter, if I use " " is works fine - but " " is not the delimiter I need. Thanks for any help in advance ;) char * token; char dir_name_out[256]; char *delims = "\\"; strcpy(dir_name_out, ""); // Clear output token = strtok(dir_name_in, delims); while(token != NULL) { if(strlen(token) > 8) { token[6] = '~'; token[7] = '1'; token[8] = '\0'; } strcat(token, "\\"); strcat(dir_name_out, token); //strcpy(token, strtok(NULL, "\\\n\0")); token = strtok(NULL, delims); } u6ik

    D G 2 Replies Last reply
    0
    • U u6ik

      Can anyone see a problem with this code? It keeps resetting the original string line so it jumps out of the loop after the first token. What it should do is parse the whole line... For some reason it doesn't seem to like the "\\" delimiter, if I use " " is works fine - but " " is not the delimiter I need. Thanks for any help in advance ;) char * token; char dir_name_out[256]; char *delims = "\\"; strcpy(dir_name_out, ""); // Clear output token = strtok(dir_name_in, delims); while(token != NULL) { if(strlen(token) > 8) { token[6] = '~'; token[7] = '1'; token[8] = '\0'; } strcat(token, "\\"); strcat(dir_name_out, token); //strcpy(token, strtok(NULL, "\\\n\0")); token = strtok(NULL, delims); } u6ik

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      If you are just searching a path for its components, why not use _splitpath() or one of the various Pathxxx() functions from the Shell API? Much better than trying to do it yourself. Also, to create a short filename from a long one, just use GetShortPathName().


      "Take only what you need and leave the land as you found it." - Native American Proverb

      U 1 Reply Last reply
      0
      • D David Crow

        If you are just searching a path for its components, why not use _splitpath() or one of the various Pathxxx() functions from the Shell API? Much better than trying to do it yourself. Also, to create a short filename from a long one, just use GetShortPathName().


        "Take only what you need and leave the land as you found it." - Native American Proverb

        U Offline
        U Offline
        u6ik
        wrote on last edited by
        #3

        Cheers Dave. Problem solved. Although.. the original problem is a poser. Still, no time to mess around when you're on a deadline ;) Thanks again. u6ik

        D 1 Reply Last reply
        0
        • U u6ik

          Cheers Dave. Problem solved. Although.. the original problem is a poser. Still, no time to mess around when you're on a deadline ;) Thanks again. u6ik

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          u6ik wrote:

          Although.. the original problem is a poser.

          What is the initial value of dir_name_in?


          "Take only what you need and leave the land as you found it." - Native American Proverb

          1 Reply Last reply
          0
          • U u6ik

            Can anyone see a problem with this code? It keeps resetting the original string line so it jumps out of the loop after the first token. What it should do is parse the whole line... For some reason it doesn't seem to like the "\\" delimiter, if I use " " is works fine - but " " is not the delimiter I need. Thanks for any help in advance ;) char * token; char dir_name_out[256]; char *delims = "\\"; strcpy(dir_name_out, ""); // Clear output token = strtok(dir_name_in, delims); while(token != NULL) { if(strlen(token) > 8) { token[6] = '~'; token[7] = '1'; token[8] = '\0'; } strcat(token, "\\"); strcat(dir_name_out, token); //strcpy(token, strtok(NULL, "\\\n\0")); token = strtok(NULL, delims); } u6ik

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            First, here's your code with the indentation restored (note: use the <pre>...</pre> tag when posting more than one line of code):

            char * token;
            char dir_name_out[256];

            char *delims = "\\";

            strcpy(dir_name_out, ""); // Clear output

            token = strtok(dir_name_in, delims);

            while(token != NULL)
            {
            if(strlen(token) > 8)
            {
            token[6] = '~';
            token[7] = '1';
            token[8] = '\0';
            }

            strcat(token, "\\");

            strcat(dir_name_out, token);

            //strcpy(token, strtok(NULL, "\\\n\0"));
            token = strtok(NULL, delims);
            }

            A couple of things about strtok. One, it modifies the original string, but nothing in your logic has an issue with that. Two, it returns a pointer within the original string. For this reason, operating on the token value is A Bad Idea. The line strcat(token,"\\");could overwrite the '\0' delimiter placed by the preceding call to strtok. You would be better off copying the token out to a separate local string, and operating on that.


            Software Zen: delete this;

            U 1 Reply Last reply
            0
            • G Gary R Wheeler

              First, here's your code with the indentation restored (note: use the <pre>...</pre> tag when posting more than one line of code):

              char * token;
              char dir_name_out[256];

              char *delims = "\\";

              strcpy(dir_name_out, ""); // Clear output

              token = strtok(dir_name_in, delims);

              while(token != NULL)
              {
              if(strlen(token) > 8)
              {
              token[6] = '~';
              token[7] = '1';
              token[8] = '\0';
              }

              strcat(token, "\\");

              strcat(dir_name_out, token);

              //strcpy(token, strtok(NULL, "\\\n\0"));
              token = strtok(NULL, delims);
              }

              A couple of things about strtok. One, it modifies the original string, but nothing in your logic has an issue with that. Two, it returns a pointer within the original string. For this reason, operating on the token value is A Bad Idea. The line strcat(token,"\\");could overwrite the '\0' delimiter placed by the preceding call to strtok. You would be better off copying the token out to a separate local string, and operating on that.


              Software Zen: delete this;

              U Offline
              U Offline
              u6ik
              wrote on last edited by
              #6

              Thanks Gary. I've made the suggested corrections and all works fine now.

              token = strtok(dir_name_in, delims);

              strcpy(buf, token);

              while(token != NULL)
              {
              if(strlen(buf) > 8)
              {
              buf[6] = '~';
              buf[7] = '1';
              buf[8] = '\0';
              }
              strcat(buf, "\\");

              strcat(dir_name_out, buf);

              token = strtok(NULL, delims);
              }

              By copying the token into buf and manipulating that, it doesn't effect the operation of the strtok function. Thanks again :) u6ik

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups