Slashr
Slashr

Reputation: 3279

How to concatenate two strings in C++?

I have a private class variable char name[10] to which I would like to add the .txt extension so that I can open the file present in the directory.

How do I go about this?

It would be preferable to create a new string variable that holds the concatenated string.

Upvotes: 150

Views: 394406

Answers (8)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 39084

C++14

using namespace std::string_literals;
std::string great = "Hello"s + " World"; // concatenation easy!

Answer to the question:

auto fname = name + ".txt"s;

If you have more than a single const char* argument then:

auto fname = ""s + path + name + ".txt";

Upvotes: 5

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361792

First of all, don't use char* or char[N]. Use std::string, then everything else becomes so easy!

Examples,

std::string s = "Hello";
std::string greet = s + " World"; //concatenation easy!

Easy, isn't it?

Now if you need char const * for some reason, such as when you want to pass to some function, then you can do this:

some_c_api(s.c_str(), s.size()); 

assuming this function is declared as:

some_c_api(char const *input, size_t length);

Explore std::string yourself starting from here:

Upvotes: 225

dtech
dtech

Reputation: 49329

There is a strcat() function from the ported C library that will do "C style string" concatenation for you.

BTW even though C++ has a bunch of functions to deal with C-style strings, it could be beneficial for you do try and come up with your own function that does that, something like:

char * con(const char * first, const char * second) {
    int l1 = 0, l2 = 0;
    const char * f = first, * l = second;

    // find lengths (you can also use strlen)
    while (*f++) ++l1;
    while (*l++) ++l2;

    // allocate a buffer including terminating null char
    char *result = new char[l1 + l2 + 1];

    // then concatenate
    for (int i = 0; i < l1; i++) result[i] = first[i];
    for (int i = l1; i < l1 + l2; i++) result[i] = second[i - l1];

    // finally, "cap" result with terminating null char
    result[l1 + l2] = '\0';
    return result;
}

...and then...

char s1[] = "file_name";
char *c = con(s1, ".txt");

... the result of which is file_name.txt.

You might also be tempted to write your own operator + however IIRC operator overloads with only pointers as arguments is not allowed.

Also, don't forget the result in this case is dynamically allocated, so you might want to call delete on it to avoid memory leaks, or you could modify the function to use stack allocated character array, provided of course it has sufficient length.

Upvotes: 9

Kartik
Kartik

Reputation: 7

//String appending
#include <iostream>
using namespace std;

void stringconcat(char *str1, char *str2){
    while (*str1 != '\0'){
        str1++;
    }

    while(*str2 != '\0'){
        *str1 = *str2;
        str1++;
        str2++;
    }
}

int main() {
    char str1[100];
    cin.getline(str1, 100);  
    char str2[100];
    cin.getline(str2, 100);

    stringconcat(str1, str2);

    cout<<str1;
    getchar();
    return 0;
}

Upvotes: -3

i.AsifNoor
i.AsifNoor

Reputation: 587

It is better to use C++ string class instead of old style C string, life would be much easier.

if you have existing old style string, you can covert to string class

    char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    cout<<greeting + "and there \n"; //will not compile because concat does \n not work on old C style string
    string trueString = string (greeting);
    cout << trueString + "and there \n"; // compiles fine
    cout << trueString + 'c'; // this will be fine too. if one of the operand if C++ string, this will work too

Upvotes: 0

TonyK
TonyK

Reputation: 17124

If you were programming in C, then assuming name really is a fixed-length array like you say, you have to do something like the following:

char filename[sizeof(name) + 4];
strcpy (filename, name) ;
strcat (filename, ".txt") ;
FILE* fp = fopen (filename,...

You see now why everybody recommends std::string?

Upvotes: 24

Madhurya Gandi
Madhurya Gandi

Reputation: 892

strcat(destination,source) can be used to concatenate two strings in c++.

To have a deep understanding you can lookup in the following link-

http://www.cplusplus.com/reference/cstring/strcat/

Upvotes: -1

nogard
nogard

Reputation: 9716

Since it's C++ why not to use std::string instead of char*? Concatenation will be trivial:

std::string str = "abc";
str += "another";

Upvotes: 41

Related Questions