memcpy replaces memory; it is not added. If you want to use memcpy, your code should be a little more complicated.
void * memcpy (void * destination, const void * source, size_t num);
When you go to mainbuf, you pass the same destination address every time. You need to increment the destination address every time you use memcpy so that it puts it in the next memory, instead of rewriting the same line every time.
Try strcat instead, it is designed to concatenate strings together, which is similar to what you want to do. Make sure you check that you have enough memory left before using it so that there are no memory problems.
source share