Syntax: char *get_input_from_stdin_and_discard_extra_characters(char *str, long size);
Function get_input_from_stdin_and_discard_extra_characters() reads at most 'size - 1' characters into 'str' from stdin and then appends the null character ('\0').
The detailed description of this function is in the header file - "get_input_from_stdin.h".
Below is the code:
get_input_from_stdin.c
#include "get_input_from_stdin.h"
#include <stdio.h>
char *get_input_from_stdin_and_discard_extra_characters(char *str, long size)
{
int c = 0;
long i = 0;
// If 'size' is 0 then this function will discard all input and return NULL.
// No need to check 'str' if 'size' is 0.
if (size == 0) {
// discard all input
while ((c = getchar()) && (c != '\n') && (c != EOF));
return NULL;
}
if (!str)
return str;
if (size < 0)
return NULL;
for (i = 0; i < (size - 1); i = i + 1) {
c = getchar();
if ((c == '\n') || (c == EOF)) {
str[i] = 0;
return str;
}
str[i] = (char)(c);
} // end of for loop
str[i] = 0;
// discard rest of input
while ((c = getchar()) && (c != '\n') && (c != EOF));
return str;
} // end of get_input_from_stdin_and_discard_extra_characters
get_input_from_stdin.h
#ifndef GET_INPUT_FROM_STDIN_H
#define GET_INPUT_FROM_STDIN_H
/*
* get_input_from_stdin_and_discard_extra_characters(char *str, long size):
*
* Function get_input_from_stdin_and_discard_extra_characters() reads at most
* 'size - 1' characters into 'str' from stdin and then appends the null
* character ('\0'). If 'size' is 0 then this function will discard all input
* and return NULL. So, to discard all input, this function can be called with
* 'str' having value NULL and 'size' having value 0.
* In all cases, reading input stops after encountering a newline ('\n') or EOF
* even if 'size - 1' characters have not been read. If a newline ('\n') or EOF
* is read then it is replaced by null character ('\0'). If there are extra
* characters in input, they are read and discarded.
* In all cases, 'str' or NULL is returned.
*/
char *get_input_from_stdin_and_discard_extra_characters(char *str, long size);
#endif
test_get_input_from_stdin.c
#include "get_input_from_stdin.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 11
int main(void)
{
char str[ARRAY_SIZE] = {0};
system("clear");
printf("\n");
if (get_input_from_stdin_and_discard_extra_characters(NULL, ARRAY_SIZE) != NULL) {
printf("Test case having 'str' value as NULL failed.\n");
} else {
printf("Test case having 'str' value as NULL passed.\n");
}
printf("\nPlease input a string: ");
if (get_input_from_stdin_and_discard_extra_characters(NULL, 0) != NULL) {
printf("Test case having 'size' value as 0 failed.\n");
} else {
printf("Test case having 'size' value as 0 passed.\n");
}
strcpy(str, "#?-^");
printf("\nPlease input a string: ");
get_input_from_stdin_and_discard_extra_characters(NULL, 0);
if (strcmp(str, "#?-^") != 0) {
printf("Test case having 'size' value as 0 and a valid 'str' pointer failed.\n");
} else {
printf("Test case having 'size' value as 0 and a valid 'str' pointer passed.\n");
}
printf("\n\nPlease press ENTER to continue..");
// now clear the stdin input buffer
get_input_from_stdin_and_discard_extra_characters(NULL, 0);
while (1) {
system("clear");
printf("\nPlease input a string (only first %d characters will be read): ", ARRAY_SIZE - 1);
get_input_from_stdin_and_discard_extra_characters(str, ARRAY_SIZE);
printf("The input received was \"%s\"\n", str);
printf("Length of input received was %zu\n", strlen(str));
printf("\n\nPlease press ENTER to continue..");
// now clear the stdin input buffer
get_input_from_stdin_and_discard_extra_characters(NULL, 0);
} // end of while(1) loop
} // end of main