Paul Manta
Paul Manta

Reputation: 31597

How does the stdin buffer work?

When using functions such as scanf you read bytes from a buffer where (usually) data coming from the keyboard is stored. How is this data stored? Is it stored inside a fixed size vector? Is there any way to access it directly from code?

Upvotes: 1

Views: 1593

Answers (3)

Pete Wilson
Pete Wilson

Reputation: 8704

You can't read the buffer directly. The best you can do is read keystrokes directly as they're typed, effectively enabling you to write your own scanf( ). To see the code for reading keystrokes, search for 'kbhit.c' on this page: http://pwilson.net/sample.html

Upvotes: 1

bk1e
bk1e

Reputation: 24328

The setvbuf() function lets you reconfigure the type of buffering for a stdio stream and replace the buffer with one you have allocated. That doesn't mean you should access the buffer behind the C library's back, but it does give you control over the size and whether the stream is unbuffered, line-buffered, or fully buffered.

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 613461

The buffer used by the standard libraries input routines is private to the implementation of the standard library. You cannot access it other than through the published interface to the standard library.

Upvotes: 3

Related Questions