How does fgets work




















Moreover, there is no safe check inside fgets. The second parameter you gave to fgets is the "safety" argument. Your program will crash because you are accessing unallocated memory. The second parameter of fgets is not the number of character you want to store but the maximum capacity of your buffer.

If you undestood the 2 answer before, you will be able to answer to this one by yourself. Try to play with this value. And use a different value than the one used for you buffer size. When you call fgets , it lets you type in as much as you want into stdin , so everything stays in stdin.

It seems fgets takes the first 9 characters, attaches a null character, and assigns it to buffer. Then puts displays buffer then creates a newline. The key is it's in a while loop -- the code loops again then takes what was remaining in stdin and feeds it into fgets , which takes the next 9 characters and repeats. Stdin just still had stuff "in queue".

There are several functions for outputting strings, such as printf and puts. Strings can be input with fgets or scanf ; however there is no standard function that both inputs and allocates memory. You need to pre-allocate some memory, and then read some characters into that memory. Your analogy of a stream as a river is not great. Rivers flow whether or not you are taking items out of them, but streams don't. A better analogy might be a line of people at the gates to a stadium.

In my analogy let's say the newline character is represented by a short person. When you do fgets buf, 20, stdin it is like "Let the next 19 people in, but if you encounter a short person during this, let him through but not anybody else".

Then the fgets function creates a string out of these 0 to 19 characters, by putting the end-of-string marker on the end; and that string is placed in buf. Note that the second argument to fgets is the buffer size , not the number of characters to read. If there were fewer than 19 people and no short people, then fgets waits for more people to arrive.

In standard C there's no way to check if people are waiting without blocking to wait for them if they aren't. By default, C streams are line buffered. In my analogy, this is like there is a "pre-checking" gate earlier on than the main gate, where all people that arrive go into a holding pen until a short person arrives; and then everyone from the holding pen plus that short person get sent onto the main gate.

This can be turned off using setvbuf. This is something you do have to worry about. Streams may end. When a stream is ended, fgets will return NULL. Your program must handle this. In my analogy, the gate is closed. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. The contents of the array pointed to by str are indeterminate it may not even be null-terminated.

POSIX additionally requires that fgets sets errno if it encounters an failure other than the end-of-file condition. Create account Log in. Namespaces Page Discussion. Views View Edit History. From cppreference. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Recommended Articles.

Article Contributed By :. Easy Normal Medium Hard Expert.



0コメント

  • 1000 / 1000