Thread: need help with strcat function

  1. #1
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37

    need help with strcat function

    Code:
    printf("%s\n", path) ;          //this print /usr/bin/
    printf("%s\n", parameter[0]) ;          //this print w
    strcat(path, parameter[0]) ;
    printf("%s\n", path) ;          //this print wusr/bin/
    
    the strcat function seems to overwrite the path rather than append to it
    any idea why?

    thanks in advance
    learning c programming for operating systems...
    learning openGL for graphics design...

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    to use strcat you have to pass two array of character parameters. Your second parameter in your strcat call seems to be a single character to me. What you trying to acheive here. If you need to append the character string parameter to path just pass parameter without the element accessor. If you wish to add the single character you cant use strcat.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    It's hard to tell from your code if parameter[0] is a char datatype or a char * datatype.
    Or if you haven't allocated enough space in path, etc.

    Please post more code.

  4. #4
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    maybe i add the declaration of the variables to be clear

    Code:
    char *parameter[100] ;
    char *path ;
    i want to append my parameter to my path string
    but i just cannot get the desired result

    my parameter is for keeping an array of pointers (each of the pointer points to a string)
    my path is just a pointer to a string
    Last edited by zell; 02-18-2005 at 11:09 AM.
    learning c programming for operating systems...
    learning openGL for graphics design...

  5. #5
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    this is the whole code if needed

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        /*check correct num of arguements*/
        if(argc != 2)
        {
             printf("Wrong number of arguments!") ;
             return 0 ;
        }
    
        FILE *f ;
        f = fopen(argv[1], "r") ;
    
        char *line[100] ;
        char *word ;
        int count = 0 ;
        
        while (!feof(f))
        {
            word = malloc(100) ;
            fgets(word, 10000, f) ;
            line[count] = word ;
            count++ ;
        }
        
        char *parameter[100] ;
        char *path ;
        char *token ;
        int count2 = 0 ;
        int c ;
        for (c = 0 ; c < count - 1 ; c++)
        {
            token = strtok(line[c], " \n") ;
            while (token != NULL)
            {
                if(!strcmp("setpath", token))
                {
                    //printf("newpath\n") ;
                    count2++ ;
                    token = strtok(NULL, " \n") ;
                    path = malloc(100) ;
                    path = token ;
                    printf("path = %s\n", path) ;
                    count2++ ;
                    token = strtok(NULL, " \n") ;
                    continue ;
                }
                parameter[count2] = token ;            
                printf("token = %s\n", parameter[count2]) ;
                count2++ ;
                token = strtok(NULL, " \n") ;
            }
            parameter[count2] = NULL ;
            count2 = 0 ;
        }
        
        //debugging
        printf("%s\n", path) ;
        printf("%s\n", parameter[0]) ;
        strcat(path, parameter[0]) ;
        printf("%s\n", path) ;
    
            
        int myid = fork() ;
    
        if (myid == 0)
        {
            execv(path, parameter) ;
            printf("Invalid command") ;
            exit(1) ;
        }
        waitpid(myid, 0, 0) ;
        printf("\n") ;
    
        fclose(f) ;
    
        return 0 ;
    }
    input file contains
    Code:
    setpath /usr/bin/
    w -s
    thanks in advance again
    Last edited by zell; 02-18-2005 at 11:32 AM.
    learning c programming for operating systems...
    learning openGL for graphics design...

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It looks like your problem might be coming from the fact that path points to memory returned by strtok(). Try using a new buffer for strcat() instead of using path.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    Quote Originally Posted by itsme86
    It looks like your problem might be coming from the fact that path points to memory returned by strtok(). Try using a new buffer for strcat() instead of using path.

    how do i do that?
    i am sorry because i am rather new to c and really dun understand the malloc and buffer thing

    thanks for help
    learning c programming for operating systems...
    learning openGL for graphics design...

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    By declaring another character array. That's all a buffer is.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    Quote Originally Posted by itsme86
    By declaring another character array. That's all a buffer is.
    as in like this?
    Code:
    char* newpath = malloc(100) ;
    newpath = strcat(path, parameter[0]) ;
    printf("%s\n", newpath) ;
    this gave the same...
    learning c programming for operating systems...
    learning openGL for graphics design...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Wow, so many mistakes

    First of all, this is C, so no mixing up declarations and statements.

    2. Don't try and cram all the functionality into main() - use some functions to do part of the work.

    > newpath = strcat(path, parameter[0]) ;
    malloc returns memory containing uninitialised data, so your cat appends to some random point in the buffer (possibly way beyond the end of it).

    > word = malloc(100) ;
    > fgets(word, 10000, f) ;
    fgets is only safe if you don't LIE about the buffer size (that's the 2nd post I've seen like this - what's going on?).

    > while (!feof(f))
    Why this is bad is in the FAQ.

    I suggest you try again, and just post your code for reading in an array of lines. Lets get that sorted out before you go off with the tokenising and calling execl.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37

    Post

    Quote Originally Posted by Salem
    Wow, so many mistakes

    First of all, this is C, so no mixing up declarations and statements.

    2. Don't try and cram all the functionality into main() - use some functions to do part of the work.

    > newpath = strcat(path, parameter[0]) ;
    malloc returns memory containing uninitialised data, so your cat appends to some random point in the buffer (possibly way beyond the end of it).

    > word = malloc(100) ;
    > fgets(word, 10000, f) ;
    fgets is only safe if you don't LIE about the buffer size (that's the 2nd post I've seen like this - what's going on?).

    > while (!feof(f))
    Why this is bad is in the FAQ.

    I suggest you try again, and just post your code for reading in an array of lines. Lets get that sorted out before you go off with the tokenising and calling execl.

    okie i have the reading of lines done
    hope i am doing it right
    Code:
     
        FILE *f ;
        f = fopen(argv[1], "r") ;
    
        char *line[100] ;
        char *word = malloc(10000) ;
        int count = 0 ;
        
        while (fgets(word, 10000, f) != NULL)
        {
            line[count] = word ;
            printf("line = %s\n", line[count]) ;
            count++ ;
            word = malloc(10000) ;
        }
        printf("count = %d\n", count) ;
    Last edited by zell; 02-19-2005 at 06:20 AM.
    learning c programming for operating systems...
    learning openGL for graphics design...

  12. #12
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    Code:
    printf("%s\n", path) ;          //this print /usr/bin/
    printf("%s\n", parameter[0]) ;          //this print w
    strcat(path, parameter[0]) ;
    printf("%s\n", path) ;          //this print wusr/bin/
    
    now i realise 1 thing
    i run this code in cygwin and it will give me wusr/bin/ for the last line of code
    however when i run it in windows cmd, it give me the correct output of /usr/bin/w

    why is this so?
    learning c programming for operating systems...
    learning openGL for graphics design...

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > why is this so?
    Probably luck.

    Do you really need to allocate 10K for every single line?

    - Read into a large buffer
    - allocate the exact space
    - copy
    - and repeat
    Code:
    char buff[BUFSIZ];
    while ( count < 100 && fgets( buff, BUFSIZ, fp ) ) {
        if ( (line[count] = malloc( strlen(buff) + 1 )) != NULL ) {
            strcpy( line[count], buff );
            count++;
        }
    }
    > f = fopen(argv[1], "r") ;
    Check return results.
    And watch how you're mixing declarations and statements, normal C doesn't (yet) allow such things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User zell's Avatar
    Join Date
    Jan 2005
    Posts
    37
    [QUOTE=Salem]> why is this so?
    Probably luck.

    cos i was thinking there is some bug with cygwin

    this is the part of the code
    Code:
        printf("path = %s\n", path[1]) ;
        printf("parameter = %s\n", parameter[0]) ;
        printf("parameter = %s\n", parameter[1]) ;
        printf("parameter = %s\n", parameter[2]) ;
        
        strcat(path[1], parameter[0]) ;
        printf("newpath = %s\n", path[1]) ;
    but the result was
    http://img169.exs.cx/img169/8896/bug2ec.jpg

    if u notice, the w actually overwrite the newpath which is not at all a variable

    so is it really a bug? or is it still my coding?

    thanks again
    learning c programming for operating systems...
    learning openGL for graphics design...

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. I can't read that image - it doesn't resolve
    2. 99.x% it's still your code.

    Snippets don't help. If you want a yes/no answer then you need a complete program we can compile and run before we can say what is happening (or might be happening).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM