I ignore the return of the local, as others have eloquently talked about this.
int len = strlen(str); char buffer[1024]; ... buffer[len] = '\n';
If strlen (str)> 1024, then this sequence will write outside the declared buffer. Also, as noted, this (possibly) will not be terminated by zero.
To safely add a new line, if you have one,
char buffer[1024]; strncpy(buffer, str, 1024); // truncate string if it is too long int len = strlen(buffer); if (len < 1022) { buffer[len] = '\n'; buffer[len + 1] = '\0'; }
Note. If the line is too long, this leaves the truncated line WITHOUT a new line.
source share