473,699 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5602
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*******@free net.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.pow ernet.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
3728
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 tilepack.c squarect.c . They are available from http://www.math.vt.edu/people/floyd/research/software/subdiv.html. I tried compiling them on my Redhat 9 box, eg cc subdivide.c. When I do so I get errors like "In function `Readtilingforvertex': :...
2
2341
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 fgets is ignored. I've been even tempted to use gets. Please,
11
3661
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 EOF/error), returns n-1 characters plus a appended null character.
6
9275
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 code is listed below #include<stdio.h> #include<stdlib.h> #include <string.h>
7
2496
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
1519
by: nozone | last post by:
Hi. The following is my code of an example: #include <stdio.h> /*line 1*/ main() /*line 2*/ {char *command = (char *)calloc(10, sizeof(char)); /*line 3*/ char *middle = (char *)calloc(10, sizeof(char)); /*line 4*/ char *name = (char *)calloc(20, sizeof(char)); /*line 5*/ char...
68
4626
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 the input until I get the needed pos. number I wrote the code , but it doesn't work the way it should : if i put some char into input, the program goes infinite loop instead of promting me to enter a new value.
20
11007
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 = "ABCDEFGHI\0"; char buff; /* Input buffer. */ char tmp1 = "ABCDEFGHI\0";
6
6843
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 *movie_list) { int option; while(option != 5) { printf("option 1. name\n");
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.