• C

fgets sscanf buffer

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 ?
LVL 1
manojantonyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Jaime OlivaresSoftware ArchitectCommented:
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

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
manojantonyAuthor Commented:
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
 
manojantonyAuthor Commented:
Am asking ... If there is an overflow in fgets ... how to eliminate the unwanted characters in buffer ?

Can u please illustrate with example ?
0
Turn Raw Data into a Real Career

There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.

 
Jaime OlivaresSoftware ArchitectCommented:
you can use:
    fflush(stdin);

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

This must be done just before the fgets() line.
0
 
aib_42Commented:
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
 
sneeuw_chanCommented:
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
 
manojantonyAuthor Commented:
Hey . what if the fgets is accepting inputs which is invalid format for scanf.

Some buffer management has to be there?
0
 
Jaime OlivaresSoftware ArchitectCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.