12

I receive a char * buffer which have the lenght of 10. But I want to concat the whole content in my struct which have an variable char *.

typedef struct{
    char *buffer;
  //..

}file_entry;

file_entry real[128];

int fs_write(char *buffer, int size, int file) {
   //every time this function is called buffer have 10 of lenght only
   // I want to concat the whole text in my char* in my struct
}

Something like this :

  real[i].buffer += buffer;

How can I do this in C ?

Valter Silva
  • 16,446
  • 52
  • 137
  • 218

3 Answers3

13

In general, do the following (adjust and add error checking as you see fit)

// real[i].buffer += buffer; 

   // Determine new size
   int newSize = strlen(real[i].buffer)  + strlen(buffer) + 1; 

   // Allocate new buffer
   char * newBuffer = (char *)malloc(newSize);

   // do the copy and concat
   strcpy(newBuffer,real[i].buffer);
   strcat(newBuffer,buffer); // or strncat

   // release old buffer
   free(real[i].buffer);

   // store new pointer
   real[i].buffer = newBuffer;
Java42
  • 7,628
  • 1
  • 32
  • 50
4

You can use strcat(3) to concatenate strings. Make sure you have allocated enough space at the destination!

Note that just calling strcat() a bunch of times will result in a Schlemiel the Painter's algorithm. Keeping track of the total length in your structure (or elsewhere, if you prefer) will help you out with that.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Always use `strncat` (also included in the link above) and not `strcat`. – pickypg Mar 17 '12 at 02:59
  • 3
    Always? That seems a bit heavy-handed. "Be careful" is always good advice, though - I'll agree with that. Here's a question and some answers regarding when to use each: http://stackoverflow.com/questions/6491038/strcat-vs-strncat-when-should-which-function-be-used – Carl Norum Mar 17 '12 at 03:00
  • 2
    If you do not know why you are using `strcat`, which is because the two buffers are under pre-determined guarantees, then you should always use `strncat`. Someone starting out in C will always be better off using `strncat` over `strcat` simply because the risk is not worth the unnoticeable reward. Considering what the benefit actually is, I would even argue that you are never going to actually benefit from it unless you're writing something incredibly low level and the pre-requisite checks have already been done. – pickypg Mar 17 '12 at 15:57
0

I am not clear. Do you want:

  • to concatenate every one of the 10 character buffers you receive into one array, pointed at by one real[0].buffer, or
  • do you want each 10 character buffer to be pointed at by a different real[i].buffer, or
  • something else?

You will need to allocate enough space for the copy of the buffer:

#include <stdlib.h>
//...
int size = 10+1; // need to allocate enough space for a terminating '\0'
char* buff = (char *)malloc(size);   
if (buff == NULL) {
    fprintf(stderr, "Error: Failed to allocate %d bytes in file: %s, line %d\n",
                     size, __FILE__, __LINE__ );
    exit(1);
}
buff[0] = '\0';    // terminate the string so that strcat can work, if needed
//...
real[i].buffer = buff;  // now buffer points at some space
//...
strncpy(real[i].buffer, buffer, size-1);
Example person
  • 3,198
  • 3
  • 18
  • 45
gbulmer
  • 4,210
  • 18
  • 20