C - How to Read String Lines from Stdin or File Memory Save
I need a version of read line that is memory save. I have this "working" solution. But I'm not sure how it behaves with memory. When I enable free(text) it works for a few lines and then I get an error. So now neither text nor result is ever freed although I malloc text. Is that correct ? And why is that so ? #include <stdio.h> #include <stdlib.h> #include <string.h> char* readFromIn() { char* text = malloc(1024); char* result = fgets(text, 1024, stdin); if (result[strlen(result) - 1] == 10) result[strlen(result) - 1] = 0; //free(text); return result; } I have A LOT of short lines to read with