I wanted to make my own getline() like function in C and this is what I came up with:
void getstring(char *string)
{
fgets(string,STRING_LENGTH,stdin);
string[strlen(string)-1]='\0';
}
It's good enough if number of typed characters doesn't exceed STRING_LENGHT (in this case 100) but once it does, everything above it is stays in buffer and jumps on next read string.
I've already tried to flush buffer by using following procedure:
void flush_buffer()
{
char c;
while((c = getchar()) != '\n' && c != EOF)
/* discard */ ;
}
It does its job in described case, but once the string doesn't exceed STRING_LENGHT I need to type anything before moving on.
Is there any way to read string such that I know whether STRING_LENGHT is exceeded or not so I can condition the flushing?
If there's a better way to make getline() like function, it would be even better.