1. You start with
strcat(data,"\n");
but what is data initialised to? Is it a large char array filled with \0, or just an uninitialised pointer?

2. Consider various combinations of \n and \r...