Code:
#include <stdio.h>
#include <string.h>
#define BSIZE 100
void add_to_buff ( char *buff, char *line ) {
int pos;
char temp[100];
sscanf( line, "%d %s", &pos, temp );
strncpy( &buff[pos], temp, strlen(temp) );
}
int main ( ) {
// this is what lines look like when you use fgets to read
// them from a file
char *tests[] = {
"4 1234\n",
"22 789\n",
"8 56\n",
"16 1234\n",
"10 789\n",
"20 56\n",
};
char bufferarray[BSIZE];
int i;
for ( i = 0 ; i < BSIZE ; i++ ) bufferarray[i] = 'x';
bufferarray[BSIZE-1] = '\0';
add_to_buff( bufferarray, tests[0] );
add_to_buff( bufferarray, tests[1] );
add_to_buff( bufferarray, tests[2] );
add_to_buff( bufferarray, tests[3] );
add_to_buff( bufferarray, tests[4] );
printf( "%s\n", bufferarray );
return 0;
}