473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

scanf hangs when taking multiple inputs in a char array

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
2 4276
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steve Zimmerman | last post by:
This post is not intended as an argument to any other post, just some simple scanf experiments that I wanted to share. I found experiments 5 and 6 the most educational. Also, I thought...
5
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one...
6
by: Pete | last post by:
Following is a fragment from a program I'm writing. I am trying to enter an 8 bit binary representation from keyboard which is then scanfed into &Ptext and then again into &Key1. This is not binary...
3
by: linguae | last post by:
Hello. In my C program, I have an array of character pointers. I'm trying to input character strings to each index of the character pointer array using scanf(), but when I run the program, I get...
17
by: Lefty Bigfoot | last post by:
Hello, I am aware that a lot of people are wary of using scanf, because doing it improperly can be dangerous. I have tried to find a good tutorial on all the ins and outs of scanf() but been...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
68
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread...
20
by: Xavoux | last post by:
Hello all... I can't remind which function to use for safe inputs... gets, fgets, scanf leads to buffer overflow... i compiled that code with gcc version 2.95.2, on windows 2000 char tmp0 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.