append/copy to end of string

This is a discussion on append/copy to end of string within the C Programming forums, part of the General Programming Boards category; Is there a way to copy or append something to the end of a string? What I specificly want to ...

  1. #1
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92

    append/copy to end of string

    Is there a way to copy or append something to the end of a string?
    What I specificly want to do is to get the current directory with getwd() (or with pwd using fork(), whatever suits my project best) and then append/copy a filename to the path. I'm sure there's a simple way to do to this. Can anyone help?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    strcat( )
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    strcat( )

    ** edit **
    Rats! You beat me.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Wow, didn't take more than one minute to get a reply. Thanks.
    A feel a bit stupid for not knowing about strcat(), though.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Here's a good reference: http://cppreference.com/

    Its got a list of all such string functions, and other Standard Library functions (for both C and C++).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Thank you, Zach, a very good list indeed. Bookmarked it.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Bookmarked that one too, thanks.

    Edit:
    Another problem in my project has come up (related to the previous one, that's why I'm not creating a new thread). I got strcat() to work, and I'm using:

    Code:
    // added the / in the filename to make it work
    strcat(&currentDir, fileName);
    
    [...]
    // the using the variable in an array
    char *argv1[] = {"touch", "input_file.txt", NULL};
    char *argv2[] = {"cat", setPath, ">>", &currentDir, NULL};
    
    [...]
    // using the arrays in the child process
    execve("/usr/bin/touch", argv1, NULL);
    execve("/bin/cat", argv2, NULL);
    The problem is my file, 'input_file.txt' is empty. My guess is that the second array is failing, but how can I make the '>>' to work?
    Last edited by kristy; 07-18-2003 at 05:50 PM.

  9. #9
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Originally posted by Salem
    Shell redirections like >> are handled by the shell, not exec level functions.
    I know, but as i.e. char *argv[] = {"ping", "-c 5", ping_host, NULL}; is equal to 'ping -c 5 apple.com' (if ping_host is set to apple.com that is) it should work, at least in theory (at least in my theory ). Or am I off track? Am I thinking wrong?
    > strcat(&currentDir, fileName);
    Your use of & is unusual - how did you declare currentDir?
    My code looks like this (and works):
    Code:
    char *fileName = { "/input_file.txt" };
    char currentDir;
    
    getwd(&currentDir);
    strcat(&currentDir, fileName);
    If this is bad code (or a bad idea to solve it), please let me know.

  10. #10
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Originally posted by Salem
    The shell assigns each word of a command line to a separate argv, so ping -c 5 apple.com would be
    char *argv[] = {"ping", "-c", "5", ping_host, NULL};
    Thanks for pointing that out. My array did work as it should, though, but it really doesn't matter if it's still done the wrong way..

    Yeah - you're overwriting memory you don't own
    Ouch, yes I am. (How could I not see that..) I applied your code to my project, thank you. What can overwriting memory like I did do to the system in the long run, btw?

    Does getwd() return an error of any sort?
    Do you mean from my code, or the function's return values?
    Last edited by kristy; 07-19-2003 at 04:51 AM.

  11. #11
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    According to the man page for getwd() (and getcwd() - which apparently should be used instead, as getwd() is for FreeBSD compatibility only) a pointer to the pathname is returned upon successful completion. If it fails a NULL pointer is returned. The functions also has five error macros EACCES, EINVAL, ENOENT, ENOMEM and ERANGE.

    *can't live without man pages*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM

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