By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

scanf hangs when taking multiple inputs in a char array

P: n/a
Adi
Hello friend,
Lately, i cam accross a small problem that has causing quite more
pain.
Scanf hangs whenever i try to re- take input into the same char array.
I've pasted below excerpt of my code:
----------------------------------------------------------------------------------------------------------------------------------------
short cond =0;
unsigned char intStr[CLI_MAX_INT16_CHARS];
/*loops until the value entered is valid*/
do
{
fflush(stdin);
scanf("%s",intStr);
cond=validateSInt(intStr);
}while(cond==1);
----------------------------------------------------------------------------------------------------------------------------------------
Given below is the stack trace of my code when i preempted it on SIGINT
(Ctrl+C). As u can see, the stack dump shows that scanf is hanged....

(gdb) where
#0 0x001e016e in __read_nocancel () from /lib/tls/libc.so.6
#1 0x0017cbb8 in _IO_file_read_internal () from /lib/tls/libc.so.6
#2 0x0017be8e in _IO_new_file_underflow () from /lib/tls/libc.so.6
#3 0x0017e5cd in _IO_default_uflow_internal () from /lib/tls/libc.so.6
#4 0x0017e27d in __uflow () from /lib/tls/libc.so.6
#5 0x00168cb4 in _IO_vfscanf_internal () from /lib/tls/libc.so.6
#6 0x0016e67a in scanf () from /lib/tls/libc.so.6

Nov 3 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Adi wrote:
Hello friend,
Lately, i cam accross a small problem that has causing quite more
pain.
Scanf hangs whenever i try to re- take input into the same char array.
I've pasted below excerpt of my code:
It's better to paste entire (minimal) examples. (If you don't understand
what's going wrong, you don't know what's important ...)
----------------------------------------------------------------------------------------------------------------------------------------
short cond =0;
unsigned char intStr[CLI_MAX_INT16_CHARS];
/*loops until the value entered is valid*/
do
{
fflush(stdin);
Undefined. BOOM. See the FAQ. To discard any "pending input", read
it.
scanf("%s",intStr);
No check for value being there. Suspect incorrect understanding of %s
format in scanf. Boomlet.
cond=validateSInt(intStr);
}while(cond==1);
----------------------------------------------------------------------------------------------------------------------------------------
Given below is the stack trace of my code when i preempted it on SIGINT
(Ctrl+C). As u can see, the stack dump shows that scanf is hanged....
No, it shows it was in scanf when you interrupted it.

--
Chris "unhashedup hashed up hashing" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Nov 3 '06 #2

P: n/a
Adi <ad******@gmail.comwrote:
Hello friend,
Lately, i cam accross a small problem that has causing quite more
pain.
Scanf hangs whenever i try to re- take input into the same char array.
I've pasted below excerpt of my code:
---------------------------------------------------------------
short cond =0;
unsigned char intStr[CLI_MAX_INT16_CHARS];
/*loops until the value entered is valid*/
do
{
fflush(stdin);
That's not allowed - you can only fflush() output, but not
input streams. There seems to be some extension under Windows,
allowing you to remove everthing from stdin by calling fflush()
on it, but you should avoid that as non-portable. If you use
it under e.g. UNIX it probably has not effect at all.
scanf("%s",intStr);
Here's a problem: if the user enters more than (CLI_MAX_INT16_CHARS -1)
non-white-space characters scanf() will write past the end the buffer
you supplied it with.
cond=validateSInt(intStr);
}while(cond==1);
------------------------------------------------------------------
Given below is the stack trace of my code when i preempted it on SIGINT
(Ctrl+C). As u can see, the stack dump shows that scanf is hanged....
(gdb) where
#0 0x001e016e in __read_nocancel () from /lib/tls/libc.so.6
#1 0x0017cbb8 in _IO_file_read_internal () from /lib/tls/libc.so.6
#2 0x0017be8e in _IO_new_file_underflow () from /lib/tls/libc.so.6
#3 0x0017e5cd in _IO_default_uflow_internal () from /lib/tls/libc.so.6
#4 0x0017e27d in __uflow () from /lib/tls/libc.so.6
#5 0x00168cb4 in _IO_vfscanf_internal () from /lib/tls/libc.so.6
#6 0x0016e67a in scanf () from /lib/tls/libc.so.6
This doesn't indicate that you're hanging in scanf(). It's just that
only while the program is waiting for user input you have a chance to
hit Ctrl-C and thus it stops somewhere in the innards of scanf().
Did you check if validSInt() ever returns anything else than 1?

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Nov 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.