There are several problems in your C code
- buff is an array for nothing because you only use
buff[0]
- the variable l seems never defined/initialized, and you modify it for nothing
buff2[count2]+=buf[i]; always modify the same buff2[count2] until a newline because you do not increase buff2 in that case but only when reading a newline, are you sure you want that ?
- you do not end buff2 with a null character, that probably explain I keep getting weird values when I print out buff2 value.
- you do not have a protection in case you write out of buff2 producing an undefined behavior
string *x=new string[10];
can be in C
char ** x = calloc(10, sizeof(char *));
I use calloc to initialize with null pointers
and an equivalent of :
x[0] += "a";
can be
strCat(&x[0], "a");
with:
char * const strCat(char ** p, const char * s)
{
if (s != NULL) {
if (*p == NULL)
*p = strdup(s);
else {
size_t len = strlen(*p);
*p = realloc(*p, len + strlen(s) + 1); /* may be detect realloc returns NULL on error */
strcpy(*p + len, s);
}
}
return *p;
}
So for instance :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * const strCat(char ** p, const char * s)
{
if (s != NULL) {
if (*p == NULL)
*p = strdup(s);
else {
size_t len = strlen(*p);
*p = realloc(*p, len + strlen(s) + 1); /* may be detect realloc returns NULL on error */
strcpy(*p + len, s);
}
}
return *p;
}
int main()
{
char ** x = calloc(10, sizeof(char *));
strCat(&x[0], "a");
strCat(&x[0], "b");
strCat(&x[0], "c");
strCat(&x[1], "x");
strCat(&x[1], "y");
strCat(&x[1], "z");
printf("x[0]=%s\n", x[0]);
printf("x[1]=%s\n", x[1]);
free(x[0]);
free(x[1]);
free(x);
return 0;
}
Compilation and execution:
% gcc -Wall a.c
% ./a.out
x[0]=abc
x[1]=xyz
%
Running under valgrind:
% valgrind ./a.out
==113490== Memcheck, a memory error detector
==113490== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==113490== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==113490== Command: ./a.out
==113490==
x[0]=abc
x[1]=xyz
==113490==
==113490== HEAP SUMMARY:
==113490== in use at exit: 0 bytes in 0 blocks
==113490== total heap usage: 7 allocs, 7 frees, 98 bytes allocated
==113490==
==113490== All heap blocks were freed -- no leaks are possible
==113490==
==113490== For counts of detected and suppressed errors, rerun with: -v
==113490== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
%
Note however each time you concatenate a new string it is needed to go through the current string to know its length, this is not done by std::string whose knows the used length whatever the way for that, as this is the case in the answer of KamilCuk
std::stringthe equivalent ofa+=b;in C can bestrcat(a,b);is C ... supposing a enough long. You use lib in C++ why not using lib too in C ? If needed you can increase result string length usingreallocl++? This is an L, not an i. Also, it seems you are adding char values together in an array - you are not concatenating strings when you dobuff2[count2]+=buf[i];.reallocandstrcat(a,b)and you can then print the pointers using%s.read(fd, buf, sizeof(buf[g]))in the best case reads only one char intobuf[0], what interrest to have an array of 255 ?print the pointers using %sis wrong, that format write the contains of a (supposed) string, use %p for a pointer