scanf hangs when taking multiple inputs in a char array

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
 
C

Chris Dollin

Adi said:
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);

No, it shows it was in scanf when you interrupted it.
 
J

Jens Thoms Toerring

Adi said:
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:
/*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);
(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top