I try to concatenate a string with an integer converted into string and write the result into a file.
There is my code (simplified):
char * convert_int_string(int val)
{
char * str = malloc(sizeof(char)*64);
sprintf(str,"%d",val);
return str;
}
char * parse_val(int val){
char * str = malloc(sizeof(char)*64);
char * str2 = convert_int_string(val);
strcat(str, "test");
strcat(str,str2);
free(str2);
return str;
}
fprintf(my_file, "%s\n", parse_val(42));
But I get this result and I don't understand why (here val is equal to 42):
��7s�test42
(I used this post How do I concatenate const/literal strings in C? and this one How to convert integer to string in C?)