Computers & Life. Giving you as much crap as possible. Good, useful crap. «somahovering, soma-hover-ing, the act body hovering»
Friday, August 10, 2012
C function for getting user input
C function for getting user input.
Reads up to uinput-1 characters from stdin to uinput, removes \n adn adds \0.
Returns number of characters of uinput.
int getUserInput(char *uinput, int size){
size_t last;
fgets(uinput, size, stdin);
if (strlen(uinput) > 0){
last = strlen(uinput) - 1;
if (uinput[last] == '\n'){ //if last char is \n replace with \0
uinput[last] = '\0';
} else {
//no \n in buffer, discard additional characters.
fscanf (stdin, "%*[^\n]");
(void) fgetc (stdin); // discard \n
}
}
return strlen(uinput);
}
Labels:
C language,
Programação,
Programming
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment