Prova questa funzione:
codice:
char* getline(const char delimiter){
char *string = NULL;
size_t i = 1;
if((string = malloc(sizeof(char))) == NULL){
puts("getline");
exit(EXIT_FAILURE);
}
while(*(string + i - 1) != delimiter && *(string + i - 1) != '\n'){
char *tmp = NULL;
*(string + i - 1) = getchar();
if((tmp = realloc(string, ++i)) == NULL){
puts("realloc error");
free(string);
string = NULL;
exit(EXIT_FAILURE);
}
string = tmp;
}
return string;
}
Questa funzione prende in input un carattere, e mette in un buffer dei caratteri, finchè non trova il delimitatore in input o il carattere NEWLINE.