I have a a char array:
char* name = "hello";
I want to add an extension to that name to make it
hello.txt
How can I do this?
name += ".txt"
won't work
I have a a char array:
char* name = "hello";
I want to add an extension to that name to make it
hello.txt
How can I do this?
name += ".txt"
won't work
Have a look at the strcat function.
In particular, you could try this:
const char* name = "hello";
const char* extension = ".txt";
char* name_with_extension;
name_with_extension = malloc(strlen(name)+1+4); /* make space for the new string (should check the return value ...) */
strcpy(name_with_extension, name); /* copy name into the new var */
strcat(name_with_extension, extension); /* add the extension */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *name = "hello";
int main(void) {
char *ext = ".txt";
int len = strlen(name) + strlen(ext) + 1;
char *n2 = malloc(len);
char *n2a = malloc(len);
if (n2 == NULL || n2a == NULL)
abort();
strlcpy(n2, name, len);
strlcat(n2, ext, len);
printf("%s\n", n2);
/* or for conforming C99 ... */
strncpy(n2a, name, len);
strncat(n2a, ext, len - strlen(n2a));
printf("%s\n", n2a);
return 0; // this exits, otherwise free n2 && n2a
}
I have a char array:
char* name = "hello";
No, you have a character pointer to a string literal. In many usages you could add the const modifier, depending on whether you are more interested in what name points to, or the string value, "hello". You shouldn't attempt to modify the literal ("hello"), because bad things can happen.
The major thing to convey is that C does not have a proper (or first-class) string type. "Strings" are typically arrays of chars (characters) with a terminating null ('\0' or decimal 0) character to signify end of a string, or pointers to arrays of characters.
I would suggest reading Character Arrays, section 1.9 in The C Programming Language (page 28 second edition). I strongly recommend reading this small book ( <300 pages), in order to learn C.
Further to your question, sections 6 - Arrays and Pointers and section 8 - Characters and Strings of the C FAQ might help. Question 6.5, and 8.4 might be good places to start.
I hope that helps you to understand why your excerpt doesn't work. Others have outlined what changes are needed to make it work. Basically you need an char array (an array of characters) big enough to store the entire string with a terminating (ending) '\0' character. Then you can use the standard C library function strcpy (or better yet strncpy) to copy the "Hello" into it, and then you want to concatenate using the standard C library strcat (or better yet strncat) function. You will want to include the string.h header file to declare the functions declarations.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
char filename[128];
char* name = "hello";
char* extension = ".txt";
if (sizeof(filename) < strlen(name) + 1 ) { /* +1 is for null character */
fprintf(stderr, "Name '%s' is too long\n", name);
return EXIT_FAILURE;
}
strncpy(filename, name, sizeof(filename));
if (sizeof(filename) < (strlen(filename) + strlen(extension) + 1) ) {
fprintf(stderr, "Final size of filename is too long!\n");
return EXIT_FAILURE;
}
strncat(filename, extension, (sizeof(filename) - strlen(filename)) );
printf("Filename is %s\n", filename);
return EXIT_SUCCESS;
}