char buffer append
-
Hi all, Im having two character buffers with different size. for ex,ist buffer is size of 40,and 2nd one is 50. what im trying to do is,create the new buffer which should contain the data from first buffer(position from 0 to 40)and the data from the 2nd buffer(position from 41 to 91). I mean the 2nd buffer's data should get append with the first buffer data. Is any way to do this?could you help me. Thanx.
-
Hi all, Im having two character buffers with different size. for ex,ist buffer is size of 40,and 2nd one is 50. what im trying to do is,create the new buffer which should contain the data from first buffer(position from 0 to 40)and the data from the 2nd buffer(position from 41 to 91). I mean the 2nd buffer's data should get append with the first buffer data. Is any way to do this?could you help me. Thanx.
gmallax wrote:
Is any way to do this?
yep
gmallax wrote:
could you help me.
will try So, if you know in advance the size of these buffers, or at least the maximum length of possible strings, then e.g. (i assumed your strings are zero-terminated, if not then tell me so):
char buffer40[40];
char buffer50[50];
...initialize your buffers with whatever content you need to...
char buffer90[40 + 50]; //I could have written 90 here instead of 40+50, it's just so for clearity's sake
strcpy(buffer90, buffer40);
strcap(buffer90, buffer50);
...both of your strings are in buffer90 now...See strcpy[^] and strcat[^] in MSDN. If you do not know the sizes of these strings in advance, or you just don't want to waste memory, then e.g.:
char first_buffer[whatever size_1];
char second_buffer[whatever size_2];
...initialize your buffers with whatever content you need to...
char *concatenated_buffer = new char[strlen(first_buffer) + strlen(second_buffer) + 1];
strcpy(concatenated_buffer, first_buffer);
strcat(concatenated_buffer, second_buffer);
...both of your strings are in concatenated_buffer now...
delete []concatenated_buffer;See strlen[^] in MSDN for info. I hope this helps, error checks are avoided for simplicity. [EDIT] I just realized i simply assumed your character buffers contain strings, which you didn't explicitly state so i might be wrong, if i am, then tell me so and we'll think of something else [EDIT]
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
-
Hi all, Im having two character buffers with different size. for ex,ist buffer is size of 40,and 2nd one is 50. what im trying to do is,create the new buffer which should contain the data from first buffer(position from 0 to 40)and the data from the 2nd buffer(position from 41 to 91). I mean the 2nd buffer's data should get append with the first buffer data. Is any way to do this?could you help me. Thanx.
-
Hi all, Im having two character buffers with different size. for ex,ist buffer is size of 40,and 2nd one is 50. what im trying to do is,create the new buffer which should contain the data from first buffer(position from 0 to 40)and the data from the 2nd buffer(position from 41 to 91). I mean the 2nd buffer's data should get append with the first buffer data. Is any way to do this?could you help me. Thanx.
If you're using C then you're going to have to use such abberations as malloc, free and memcpy. If you're using C++ you can get away with using a vector - it can be used just about anywhere a built in array can be used and doesn't involve memory management. If the two buffers are called buffer_1 and buffer_2 (and they're arrays and not some block of memory bunged on the heap):
std::vector<char> appended_buffer( &buffer_1[ 0 ], &buffer_1[ 40 ] );
appended_buffer.resize( appended_buffer.size() + (&buffer_2[ 50 ] - &buffer_2[ 0 ]) );
std::copy( &buffer_2[ 0 ], &buffer_2[ 50 ], &appended_buffer[ 40 ] );The big problem with this is you have to know the sizes of the buffers you're copying from. You can get around this by never using raw character buffers and using vectors for everything. If buffer_1 and buffer_2 are vectors:
std::vector<char> appended_buffer( buffer_1.size() + buffer_2.size() );
std::copy( buffer_2.begin(), buffer_2.end(),
std::copy( buffer_1.begin(), buffer_1.end(), appended_buffer.begin() ) );The nested copying takes uses the fact that std::copy returns the place (actually it's an iterator) where the second copy has to start - really handy. All this is a lot easier than messing about with manually managing memory. Well, unless you're a masochist. Cheers, Ash