Solved

fgets sscanf buffer

Posted on 2004-08-26
8
1,053 Views
Last Modified: 2008-02-01
Hi..

there s a problem with scanf when reading a string to char array... if the length of input is greater than the allocated size of the character array..

somebody in the group posted..
fgets , sscanf and the below functions can be used instead of scanf.
void clearbuffer()
{
   int junk;
   do
   {
      junk = getchar();
   } while (junk != '\n' && junk != EOF && junk != '\0');
}

can anyone explain ?
0
Comment
Question by:manojantony
8 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 30 total points
ID: 11902624
This function is not intended for what you want to do. It only reads pending character in keyboard buffer, it is used before calling an input function to avoid typing errors.

you can use fgets() to read an entire line limiting size, so buffer size will be not a problem.
If the line you read have many values you want to "scanf" then use sscanf():

#define MAX_SIZE 1000

char buffer[MAX_SIZE];
fgets(buffer, MAX_SIZE, stdin);  /* reads from standard input (console) */

Optionally:
sscanf(buffer, "%i %f %s", &myInteger, &myFloat, anotherCharBuffer);
0
 
LVL 1

Author Comment

by:manojantony
ID: 11905327
Yes thats fine .. but .. I am not able to clear the buffer.

Suppose the MAX_SIZE is 10 and am enetring 13 characters. In the buffer there will not be any '\n' as well as '\0'. Also 3 characters are extra.

And if am not able to clear the buffer, those 3 will be coming in the next output.

I hope the question is clear.

whats the solution ?
 
0
 
LVL 1

Author Comment

by:manojantony
ID: 11909033
Am asking ... If there is an overflow in fgets ... how to eliminate the unwanted characters in buffer ?

Can u please illustrate with example ?
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11909081
you can use:
    fflush(stdin);

or you can use the clearbuffer() function you posted.

This must be done just before the fgets() line.
0
 
LVL 7

Expert Comment

by:aib_42
ID: 11915240
Bear in mind that fflush(stdin) causes undefined behaviour on some systems, and that there is no standard way to flush the input buffer. Taking a look at the available ioctl() calls might help, as might jaime's suggestions do.
0
 
LVL 2

Assisted Solution

by:sneeuw_chan
sneeuw_chan earned 20 total points
ID: 11956384
The 'clean' way to do this would be to make a function that allocates as much memory as you need to read an entire line no matter what the length is.  Lots of systems already have a function like that, for example BSD has an fgetln() function.  If not, you can quite easily make a small routine yourself that uses fgets and realloc to read lines as long as the user types them.
0
 
LVL 1

Author Comment

by:manojantony
ID: 12149480
Hey . what if the fgets is accepting inputs which is invalid format for scanf.

Some buffer management has to be there?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12150374
It is the same effect than use scanf, string is not evaluated until user presses enter.
sscanf returns number of arguments processed correctly, so it is a primary kind of control.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Finding a good hash function 4 127
How to creat good software interface ? 1 69
How to learn Linux? 10 67
If a cell in a range equals "YES" return specific Test. 4 37
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question