ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Fgets(), I understand, has the advantage to prevent buffer overflow as a result of the buffer size inclusion in its arguments. However, if I enter a string of >99 chars in the above program, the first 99 chars are displayed in one string, and then the remaining chars which I entered are returned in a following string. If the buffer established is only SIZE ==100, then where are the other chars that I entered getting stored. Is a new buffer being created after I exceed 99 chars, or what is happening to store these additional chars?
Thanks for any attempts at explanation.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
fgets() will read at most one less than sizeof (buf) characters (it will terminate the string with a null); it will read to a NL character or to one less than the size of the buffer (the NL character will be included in the buffer and the string will be terminated with a NULL).
Your loop reads 99 characters, prints them, then reads whatever is left in the system buffer (not your buffer, the I/O buffer maintained by the system) and prints those.
A slightly more "standard" way might be something like this (which is reading from a file rather than the keyboard):
The token, BUFSIZ, is a numeric value (defined as 8192 on many 64-bit systems) -- it doesn't hurt anything (and can be beneficial) to use BUFSIZ for I/O buffering as above; 8K isn't a heck of lot of memory to allocate for this purpose (and you can use the same buffer space again and again throughout a program).
Also, read the manual page for fgets for more information.
Hope this helps some.
[EDIT]
Duh! I typed NULL instead of NL (fixed above), fumble-fingers!
fgest() will read to a NL, EOF or to one less than size.
NL is the ASCII abbreviation of new line, ASCII EOT is usually EOF in Unix/Linux.
There may be a few buffers between your keyboard and your code. In particular, if you are just typing on your terminal, it is very likely that it is set in line-buffering mode which means that it won't send any data until you press Return.
Then, the TTY device in the kernel will read data to its own buffer and offer it on standard input of your program.
If you've typed more then the TTY driver is willing to handle in one go, then it may read only part of the data and then block in which case the data that has not yet been consumed will still reside in terminal's buffer.
Of course things get much more complicated if you are connected via SSH.
It is also worth nothing that as tronayne has said, 8K isn't a heck of a lot of memory, and if you are reading data from a file, then kernel will most likely read in chunks of multiples of 4K anyway. So a simple fgetc() which returns a single byte may cause operating system to read 4K page anyway.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
If you're serious about C programming, let me recommend an excellent book you might want to pick up: Stephen G. Kochan, Patrick H. Wood, Topics in C Programming (revised ed.). The ISBN is 0-471-53404-8 and it is available from Amazon and other providers (there may be a newer edition).
It's written to teach C programmers how to program and, in my opinion, is the best single-source guide available detailing advanced C programming for a Unix/Linux environment. There are hundreds of working examples (yeah, really, working useable examples). Kochan and Wood come from Bell Labs and write clearly and concisely -- it's an easy read and well worth your time.
I would urge that you write programs to ANSI/POSIX standards -- if you do, they're going to work on pretty much any platform you may need to support. Avoid "handy" extensions -- they're expedient when programming but will come back and haunt you if you need to port from, say, Linux to Solaris (Solaris' C compiler is not GNU). Stick with the standards and you won't be reinventing the wheel (and getting telephone calls at three in the morning, either). And, if you need to port to Microsoft, heaven help you if you don't stick to standards.
it will terminate the string with a null); it will read to a NULL character or to one less than the size of the buffer
Also, read the manual page for fgets for more information.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.