How to combine two lines 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 should I do it? It would be preferable to create a new string variable containing the concatenated string.

+43
c ++
Mar 10 '13 at 7:13
source share
7 answers

First of all, do not use char* or char[N] . Use std::string , then everything else will become so simple!

Examples

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

Easy, right?

Now, if you need char const * for some reason, for example, when you want to go to some function, 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:

Hope this helps.

+82
Mar 10 '13 at 7:15
source share

Since this is C ++, why not use std::string instead of char* ? Concatenation will be trivial:

 std::string str = "abc"; str += "another"; 
+10
Mar 10 '13 at 7:15
source share

If you programmed in C, then provided that name really a fixed-length array, as you say, you need to do something like the following:

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

Now you see why everyone recommends std::string ?

+5
Mar 10 '13 at 7:29
source share

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

By the way, although C ++ has a bunch of functions for working with C-style strings, it may be useful if you try to find your own function that does this, for example:

 char * con(const char * first, const char * second) { int l1 = 0, l2 = 0; const char * f = first, * l = second; // step 1 - find lengths (you can also use strlen) while (*f++) ++l1; while (*l++) ++l2; char *result = new char[l1 + l2]; // 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"); 

... whose result is file_name.txt .

You might also be tempted to write your own operator + , but only overload the IIRC operator with pointers, because the arguments are not allowed.

Also, do not forget that the result is dynamically allocated in this case, so you may want to delete it to avoid memory leaks, or you can change the function to use the character array allocated by the stack, provided that it has sufficient length.

+4
Mar 10 '13 at 7:18
source share

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

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

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

0
Feb 10 '17 at 17:02
source share

It is better to use a C ++ string class instead of an old-style C string, life will be much easier.

if you have an existing old style string, you can hide the 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 
0
25 Oct '17 at 12:37 on
source share
 //String appending #include<iostream> using namespace std; void stringconcat(char *str1, char *str2){ while (*str1 != '\0'){ str1++; } while(*str2 != '\0'){ *str1 = *str2; str1++; str2++; } return ; } int main( ){ char str1[100]; cin.getline(str1,100); char str2[100]; cin.getline(str2,100); stringconcat(str1,str2); cout<<str1; getchar(); return 0; } 
-2
Jun 23 '15 at 19:22
source share



All Articles