W
White Spirit
I'm trying to use getchar() to read alphanumeric data as follows:-
char input[150];
/* Take a string of input and remove all spaces therein */
int j = 0;
while ((input[j] = getchar()) != '\n')
{
if (!isspace(input[j]))
j++;
}
/* Append the null character */
input[j] = '\0';
I'm aware there's no bounds checking at present - it's forms part of
test code at present. The problem is, I get unexpected behaviour when
reading digits. With Linux and Solaris, if the data starts with a digit
the programme hangs. With Linux, if the stream begins with an alpha
character, it works as intended but on the Solaris box I get entirely
different characters.
I've looked in books and on Google but nothing is specifically mentioned
about this. I assume that getchar() is intended for alpha data only,
which presumably means that using scanf would be better with a separate
function to remove the whitespace?
char input[150];
/* Take a string of input and remove all spaces therein */
int j = 0;
while ((input[j] = getchar()) != '\n')
{
if (!isspace(input[j]))
j++;
}
/* Append the null character */
input[j] = '\0';
I'm aware there's no bounds checking at present - it's forms part of
test code at present. The problem is, I get unexpected behaviour when
reading digits. With Linux and Solaris, if the data starts with a digit
the programme hangs. With Linux, if the stream begins with an alpha
character, it works as intended but on the Solaris box I get entirely
different characters.
I've looked in books and on Google but nothing is specifically mentioned
about this. I assume that getchar() is intended for alpha data only,
which presumably means that using scanf would be better with a separate
function to remove the whitespace?