2

I have two string constants const char * like this:

const char * p1 = "abcd";
const char * p2 = "efgh";

I want to convert these into a single string so that it becomes a file name:

const char * filename = "abcd_efgh.txt";

I tried to concatenate the char * but failed. Kindly guide me as to how to do this.

Thanks

Clifford
  • 82,791
  • 12
  • 81
  • 153
tariq
  • 531
  • 5
  • 19
  • 34

6 Answers6

4

char* are pointers, i.e they hold the address of the memory segment where the data is stored. You need to allocate a new, large enough buffer and then use the strcat() function to concatenate the strings.

This is really the C way to do this, not the C++ way. In C++ you should use a string class, such as std::string which handles all the buffer allocation stuff for you.

Anders Abel
  • 66,163
  • 17
  • 148
  • 213
3

I would go with sprintf()

char buffer[strlen(p1) + strlen(p2) + 6];
sprintf(buffer, "%s_%s.txt", abcd, efgh);

(You're adding 6 for the _, .txt, and the \0 to terminate the string; 1 + 4 + 1)

Clifford
  • 82,791
  • 12
  • 81
  • 153
Brian Roach
  • 74,513
  • 12
  • 132
  • 160
  • 1
    Change it to `snprintf`. `sprintf` is difficult to use safely, deprecated, and should not be used. – R.. GitHub STOP HELPING ICE Mar 27 '11 at 08:52
  • Meh, we somehow managed to be able to count prior to c99. I don't find basic arithmetic daunting. – Brian Roach Mar 27 '11 at 09:49
  • @Brain: ... except that the variable-length array rather implies C99. If p1 and p2 where const-arrays rather than const-pointers, `sizeof` could be used in C89/90 or C99. – Clifford Mar 27 '11 at 10:19
  • @R.: Note that `sprintf` is none more or less safe than `snprintf` in this case, because `strlen` is not. You allocate a buffer to concat the strings into. `sprintf` could fail, but only if the buffer is not large enough (string not properly zero-terminated). But then, `strlen` would already fail (and, on top of that, allocating a buffer of possibly unlimited size on the stack can cause a crash too). – Damon Mar 27 '11 at 10:43
  • @R: `sprintf` has been deprecated by who? – 6502 Mar 27 '11 at 16:57
  • You could spell the `6` as `sizeof "_.txt"`, to make it clear where it comes from. – caf Mar 28 '11 at 00:38
1

You could use the strcat function:

/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}
Merlyn Morgan-Graham
  • 56,626
  • 16
  • 121
  • 179
0

You have declared the filename string const, which is too restrictive for what you want to do at runtime, but can be done by the pre-processor at compile time, using the adjacent string rule:

#define PART1 "abcd"
#define PART2 "efgh"

const char* p1 = PART1 ;
const char* p2 = PART2 ;

const char* filename = PART1 "_" PART2 ".txt"

However if you don't need filename to be a const use one of the already proposed solutions.

Clifford
  • 82,791
  • 12
  • 81
  • 153
  • Actually it was my edit that added `const`. Assigning a string literal address to a non-const `char *` is bad style that is accepted just because of backward compatibility (some compiler even give a diagnostic message for it). – 6502 Mar 27 '11 at 16:52
0
const std::string p1 = "abcd";
const std::string p2 = "efgh";

std::string filename = p1 + "_" + p2 + ".txt";
BenjaminB
  • 1,727
  • 3
  • 15
  • 32
-2

Try this:

char name1[] = "my_demo";
char name2[] = "_file.txt";

char* filename = (char*) malloc(sizeof(char) * (strlen(name1) + strlen(name2) + 1));
strcpy(filename, name1);
strcat(filename, name2);

printf("Filename is: %s \n", filename);

free(filename);

Outputs:

Filename is: my_demo_file.txt
karlphillip
  • 89,883
  • 35
  • 240
  • 408
  • 2
    -1: this is bad for two reasons: 1) `sizeof(name1)` is 4 by pure coincidence and not because those strings have 4 chars, 2) there is no need to set the ending NUL, `strcat` does that already. – 6502 Mar 27 '11 at 08:36
  • It's still somewhat broken - you need to change `sizeof` to `strlen` in two places, otherwise you are allocating too many bytes. Also sizeof(char) == 1 by definition, so this is redundant. – Paul R Mar 27 '11 at 14:54
  • Great, thanks @Paul! Regarding `sizeof(char)`, it's a style issue. – karlphillip Mar 27 '11 at 15:11
  • Removed my -1: but I agree with Paul that `sizeof(char)` simply shows you may think it's not 1. – 6502 Mar 27 '11 at 16:50
  • http://stackoverflow.com/questions/2215445/are-there-machines-where-sizeofchar-1/2215454#2215454 – karlphillip Mar 27 '11 at 16:53