1. Why do you cast the result of malloc? In C we normally write it like this

char *txt = malloc(sizeof(char));

2. The above means to allocate 1 character. Is that big enough for your input??
...