Thread: Why are some elements missing after using strcat()

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Why are some elements missing after using strcat()

    Hello,

    I am trying to split the unix folder path from the file name that is entered.

    For example, if the path entered is "/home/user/temp/asdf.txt"

    I want to return 2 things...
    Folder name: "/home/user/temp"
    File Name: "asdf.txt"


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define WORD_COUNT 10
    
    int main (int argc, char *argv[] )
    {
      char text[] = "/The/quick/brown/fox/jumped/over/the/lazy/red/dog";
      char *wlist[WORD_COUNT]; /*array of 10 pointers to characters*/
      char *nwlist[WORD_COUNT];
      int i;
    
    
      printf("text[%s]\n", text);
    
    
      wlist[0] = strtok( text, "/" );
      nwlist[0] = strtok( text, "/" );
    
    
      for ( i=1; i < WORD_COUNT; i++ )
    {
        wlist[i] = strtok( NULL, "/" );
    }
    
      for ( i=0; i < WORD_COUNT-1; i++ )
    {
        strcat(wlist[i], "/");
        printf("\n%s",nwlist[i]);
    }
    
      return 0;
    }
    On running this, I get the following output...

    Code:
    text[/The/quick/brown/fox/jumped/over/the/lazy/red/dog]
    
    The/
    /
    brown/
    /
    jumped/
    /
    the/
    /
    I have used " \n" to ensure readibility...

    Kindly help me if i am wrong anywhere...

    Thank you.

    -Monil

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by monil
    I am trying to split the unix folder path from the file name that is entered.

    For example, if the path entered is "/home/user/temp/asdf.txt"

    I want to return 2 things...
    Folder name: "/home/user/temp"
    File Name: "asdf.txt"
    I'd choose strrchr to look for the last '/' and overwrite it with a null.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char text[] = "/home/user/temp/asdf.txt";
       char *file, *path = text, *slash = strrchr(text, '/');
       printf("text = \"%s\"\n", text);
       if ( slash )
       {
          *slash = '\0';
          file = ++slash;
       }
       printf("path = \"%s\"\n", path);
       printf("file = \"%s\"\n", file);
       printf("text = \"%s\"\n", text);
       return 0;
    }
    
    /* my output
    text = "/home/user/temp/asdf.txt"
    path = "/home/user/temp"
    file = "asdf.txt"
    text = "/home/user/temp"
    */
    [edit]But to try to answer your title question, would looking at this help?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define WORD_COUNT 10
    
    void show(const char *text, size_t size)
    {
       while(size--)
       {
          if(isprint(*text))
          {
             putchar(*text);
          }
          else
          {
             printf("%d", *text);
          }
          ++text;
       }
    }
    
    int main(void)
    {
       char text[] = "/The/quick/brown/fox/jumped/over/the/lazy/red/dog";
       char *wlist[WORD_COUNT]; /*array of 10 pointers to characters*/
       char *nwlist[WORD_COUNT];
       int i;
    
       show(text, sizeof text);
       puts("\n---");
    
       wlist[0] = strtok( text, "/" );
       show(text, sizeof text);
       printf(", wlist[%d] = \"%s\"\n", 0, wlist[0]);
    
       //nwlist[0] = strtok( text, "/" );
       //show(text, sizeof text);
    
       for ( i=1; i < WORD_COUNT; i++ )
       {
          wlist[i] = strtok( NULL, "/" );
          show(text, sizeof text);
          printf(", wlist[%d] = \"%s\"\n", i, wlist[i]);
       }
       puts("---");
    
       for ( i=0; i < WORD_COUNT-1; i++ )
       {
          strcat(wlist[i], "/");
          show(text, sizeof text);
          printf(", wlist[%d] = \"%s\"\n", i, wlist[i]);
       }
    
       return 0;
    }
    
    /* my output
    /The/quick/brown/fox/jumped/over/the/lazy/red/dog0
    ---
    /The0quick/brown/fox/jumped/over/the/lazy/red/dog0, wlist[0] = "The"
    /The0quick0brown/fox/jumped/over/the/lazy/red/dog0, wlist[1] = "quick"
    /The0quick0brown0fox/jumped/over/the/lazy/red/dog0, wlist[2] = "brown"
    /The0quick0brown0fox0jumped/over/the/lazy/red/dog0, wlist[3] = "fox"
    /The0quick0brown0fox0jumped0over/the/lazy/red/dog0, wlist[4] = "jumped"
    /The0quick0brown0fox0jumped0over0the/lazy/red/dog0, wlist[5] = "over"
    /The0quick0brown0fox0jumped0over0the0lazy/red/dog0, wlist[6] = "the"
    /The0quick0brown0fox0jumped0over0the0lazy0red/dog0, wlist[7] = "lazy"
    /The0quick0brown0fox0jumped0over0the0lazy0red0dog0, wlist[8] = "red"
    /The0quick0brown0fox0jumped0over0the0lazy0red0dog0, wlist[9] = "dog"
    ---
    /The/0uick0brown0fox0jumped0over0the0lazy0red0dog0, wlist[0] = "The/"
    /The//0ick0brown0fox0jumped0over0the0lazy0red0dog0, wlist[1] = "/"
    /The//0ick0brown/0ox0jumped0over0the0lazy0red0dog0, wlist[2] = "brown/"
    /The//0ick0brown//0x0jumped0over0the0lazy0red0dog0, wlist[3] = "/"
    /The//0ick0brown//0x0jumped/0ver0the0lazy0red0dog0, wlist[4] = "jumped/"
    /The//0ick0brown//0x0jumped//0er0the0lazy0red0dog0, wlist[5] = "/"
    /The//0ick0brown//0x0jumped//0er0the/0azy0red0dog0, wlist[6] = "the/"
    /The//0ick0brown//0x0jumped//0er0the//0zy0red0dog0, wlist[7] = "/"
    /The//0ick0brown//0x0jumped//0er0the//0zy0red/0og0, wlist[8] = "red/"
    */
    The nulls are shown as zero in the above.

    The calls to strcat don't just write the character '/', they write the string "/", which is a '/' and a '\0'.
    Last edited by Dave_Sinkula; 03-08-2005 at 12:22 PM. Reason: Added attempt at explaining strcat issue.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM