473,387 Members | 1,812 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,387 software developers and data experts.

fgets() and scanf() peculiarity

Hey, all groovy C programmers,

I've read in the FAQ(question 12.18) about how applications skip calls to
(f)gets() after scanf() has been used. How can I avoid this? I know that I
can by putting the fgets() before the scanf(). However, this one is not
always suitable. Do you know of any other ways of avoiding this problem?

--
- Fordi det rotar til måten folk
vanlegvis les tekst på.
- Kvifor?
- Topp-posting.
- Kva er det verste du veit om
i elektronisk post og njus?
Nov 14 '05 #1
6 5587
On Wed, 10 Dec 2003 21:02:12 +0100, "Eirik"
<hx******************************@xyxaxhxoxo.no> wrote:
Hey, all groovy C programmers,

I've read in the FAQ(question 12.18) about how applications skip calls to
(f)gets() after scanf() has been used. How can I avoid this? I know that I
can by putting the fgets() before the scanf(). However, this one is not
always suitable. Do you know of any other ways of avoiding this problem?


Read it again. Notice that it doesn't say that the call to gets() is
actually skipped, only that it may appear so. Understand why this
happens, and you'll have your answer.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #2
Eirik wrote:

Hey, all groovy C programmers,

I've read in the FAQ(question 12.18) about how applications skip calls to
(f)gets() after scanf() has been used. How can I avoid this? I know that I
can by putting the fgets() before the scanf(). However, this one is not
always suitable. Do you know of any other ways of avoiding this problem?


One way is to use fgets() -- not gets()! -- to read
a whole line into a character array, then use sscanf()
to pick it apart.

--
Er*********@sun.com
Nov 14 '05 #3
> > Hey, all groovy C programmers,

I've read in the FAQ(question 12.18) about how applications skip calls to (f)gets() after scanf() has been used. How can I avoid this? I know that I can by putting the fgets() before the scanf(). However, this one is not
always suitable. Do you know of any other ways of avoiding this problem?
One way is to use fgets() -- not gets()! -- to read
a whole line into a character array, then use sscanf()
to pick it apart.


Can you explain this with a piece of code?
--
Er*********@sun.com

Nov 14 '05 #4
"Eirik" <hx******************************@xyxaxhxoxo.no> wrote:
Hey, all groovy C programmers,

I've read in the FAQ(question 12.18) about how applications skip calls to
(f)gets() after scanf() has been used. How can I avoid this? I know that I
can by putting the fgets() before the scanf(). However, this one is not
always suitable. Do you know of any other ways of avoiding this problem?


The call to fgets isn't skipped, but returns immediately when it hits
a newline character that was left in the input stream by a previous
invokation of scanf. One way to avoid this is to 'drain' the input
buffer before fgets is called:

int drain_stdin( void )
{
int c;
while ( ( c = getchar() ) != '\n' && c != EOF )
;
return c;
}

.....
.....
scanf( .... );
if ( drain_stdin() != EOF )
fgets( .... );
.....
.....

Or, you can replace the call to scanf with fgets to read an entire
line of input and then parse it. (Look up the sscanf, strto*, strchr,
strcspn, strpbrk, strrchr, strspn, strstr, strtok, is*, strncpy, ...
functions in your library reference manual.)

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.angelfire.com/ms3/bchambl...me_to_clc.html
clc faq-list : http://www.eskimo.com/~scs/C-faq/top.html
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...acllc-c++.html
Nov 14 '05 #5
Eirik wrote:
> > Hey, all groovy C programmers,

I've read in the FAQ(question 12.18) about how applications skip calls to (f)gets() after scanf() has been used. How can I avoid this? I know that I can by putting the fgets() before the scanf(). However, this one is not
always suitable. Do you know of any other ways of avoiding this problem?


One way is to use fgets() -- not gets()! -- to read
a whole line into a character array, then use sscanf()
to pick it apart.


Can you explain this with a piece of code?


char buffer[BIG_ENOUGH];
int hh, mm, dd;

if (fgets(buffer, sizeof buffer, stdin) == NULL) {
/* end-of-file or error: do something */
}
if (sscanf(buffer, "%d:%d:%d", &hh, &mm, &ss) != 3) {
/* something strange in the input: do something */
}
/* Three numbers have been retrieved from one input
* line, and the next fgets() will get a fresh line
*/

Fancier code would check whether fgets() actually read a
complete line; if `BIG_ENOUGH' is too small, fgets() will
stop prematurely. Corrective action might be to ignore the
rest of the line, or to expand a dynamically-allocated
`buffer' and keep on reading.

--
Er*********@sun.com
Nov 14 '05 #6
Eric Sosman wrote:
Fancier code would check whether fgets() actually read a
complete line; if 'BIG_ENOUGH' is too small, fgets() will
stop prematurely.


BIG_ENOUGH is always big enough. Just avoid TOO_MUCH, because TOO_MUCH is
never enough (cf Steinman, 1981).

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Stuart Anderson | last post by:
I am looking to use Ken Stephenson's CirclePack program (http://www.math.utk.edu/~kens/) with some tiling programs written by Cannon, Floyd, Parry. The programs I want to use are subdivide.c...
2
by: Diego | last post by:
Hi, Using gcc 2.96 This message was suggested by a thread started by Knak on 21/03/04 The question is: When I run the following code, if I want to introduce a second pile of data, the...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
6
by: Harini | last post by:
Ive written this sample code in Dev C++ and use fgets to read from a external file. My compiler crashes with a windows error. Could somebody help me out a sample of the usage of of fgets in my...
7
by: RSoIsCaIrLiIoA | last post by:
until a poor newbie can build a better function than sscanf and fgets scanf("%s", string) is like gets(string)
1
by: nozone | last post by:
Hi. The following is my code of an example: #include <stdio.h> /*line 1*/ main() ...
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 =...
6
by: primpa | last post by:
Hi, i have a problem with a menu, it wont stop at the fgets function. when i run the program and choose case 1 or 2 i just printout's the printf functions. int function(struct movie...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.