Talk:c/io/fgets
From cppreference.com
[edit] errno use
Shouldn't the implicit errno use (by perror()) in the example be removed? Since I can't find any obligation in the C std to set the errno by the stdio functions.
[edit] citation needed: undefined behavior for count < 1
I cannot find a word about this sentence in either ISO C (n1256.pdf) or ISO C++ (n4296.pdf). So where does this sentence come from?
- as the edit history points out, this came from http://stackoverflow.com/questions/23388620 --Cubbi (talk) 19:22, 26 January 2017 (PST)
- I disagree. The language in the standard is quite clear, and leaves no room for undefined behavior. If n==1, then fgets will read zero characters into the array, and thus there is no character in the array after which a null character must be pl
aced; similarly, if n<1, then the function must read into the array at most a negative number of characters, which is impossible, and thus no character is placed in the array after which a null character must be placed. How is this not well defined? 97.116.72.10 21:08, 22 April 2020 (PDT)
- clearly others (including library implementors) interpret the text differently, thus it is demonstrably unclear. I just surveyed a few libs, some document what they do explicilty in docs, some in source. Pending counter-examples, to fulfill cppreference mission of being useful to programmers (as opposed to language lawyers), it's more important to say what actually happens. --Cubbi (talk) 07:52, 27 April 2020 (PDT)
- Well, I agree with that policy. I'll just note that I don't think even library implementors bother to read the Standard—when you're an expert, you start to trust your own memories instead; for instance,
info '(libc)Line Input'
states:- The ‘fgets’ function reads characters from the stream STREAM up to and including a newline character and stores them in the string S, adding a null character to mark the end of the string. You must supply COUNT characters worth of space in S, but the number of characters read is at most COUNT − 1. The extra character space is used to hold the null character at the end of the string.
- That ain't Standard C. That's, I don't know, Richard Stallman C. Too bad the Standard C folks don't seek to resolve silly discrepancies like this. 97.116.72.10 17:16, 27 April 2020 (PDT)
- Well, I agree with that policy. I'll just note that I don't think even library implementors bother to read the Standard—when you're an expert, you start to trust your own memories instead; for instance,