?
KnufflPuffl schrieb:
dankeschön, dass schreib ich mir ne methode, is sogar ne bessere Lösung!
Eine was?
#include <stdio.h>
#define SMAXLEN BUFSIZ
int add_char ( char** s, size_t* old_len, char c ) {
char* _new = realloc ( *s, *old_len+2 );
if ( _new == NULL ) return 1;
_new[*old_len] = c, _new[*old_len+1] = 0;
*s = _new, *old_len += 1;
return 0;
}
// Rückgabewerte: 0 alles ok, 1 kein RAM, 2 SMAXLEN überschritten.
int read_stdin ( char** in ) {
int c = 0; size_t len = 0;
while ( ( c = getchar() ) != '\n' && c != EOF && len <= SMAXLEN )
if ( add_char( in, &len, (char)c ) ) return 1;
if ( len > SMAXLEN ) return 2;
return 0;
}
int main() {
char* line = NULL;
int echo_on = 1;
printf ( "Gebe er maximal %d Zeichen ein: ", SMAXLEN );
read_stdin ( &line );
if ( line && echo_on ) puts ( line );
free ( line );
}
:p