Thread: Combine two strings without modifying them

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    Combine two strings without modifying them

    Hey,
    I've been trying to combine two strings into a third one without modifying the original two. Heres the code I've got, but it wont compile:

    char rho_dir[50], vel_dir[50];
    char outputpath[50] = "./path/to/directory/"

    rho_dir = strcpy(rho_dir,strcat(strdup(outputpath),"fields_r ho"));
    vel_dir = strcpy(vel_dir,strcat(strdup(outputpath),"fields_v el"));

    I want to set the variables rho_dir = "./path/to/directory/fields_rho" and vel_dir = "./path/to/directory/fields_vel" without changing the string outputpath. Am I being insanely thick but I can't see an easy way to do it.

    Thanks alot,
    Kieran

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. use strcpy + strcat
    2. use sprintf

    Code:
    sprintf(rho_di,"%s%s", outputpath,"fields_rho");
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  2. how do I combine two strings together
    By freeman_peterso in forum C Programming
    Replies: 19
    Last Post: 06-27-2003, 01:35 PM
  3. How to combine 2 strings ?
    By The-Guy in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2002, 09:10 PM
  4. Help on modifying strings! Please. : - )
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 05-15-2002, 02:08 PM
  5. modifying strings
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2001, 12:36 PM