Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Is C99 the final C?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Irrwahn Grausewitz, post: 1692490"] fgets is *not* a string function, it's an input function. fgets is used to read lines of characters from text streams. fgets is not used to read C strings from binary streams. The only valid line terminator in C text streams is '\n'. Everything else is to be considered part of the line. For this reason fgets takes no special action on '\0' characters, because they have no special meaning in text streams. [1] IMNSHO a file containing '\0's is not a text file but a binary file, and should be treated as such. If your text files contain spurious NUL characters your files are broken, and you cannot blame fgets for this. Please reconsider the restrictive definition of text streams in the standard: ISO/IEC 9899:1999 7.19.2 Streams 2 A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. [...] Data read in from a text stream will necessarily compare equal to the data that were earlier written out to that stream only if: the data consist only of printing characters and the control characters horizontal tab and new-line; no new-line character is immediately preceded by space characters; and the last character is a new-line character. Whether space characters that are written out immediately before a new-line character appear when read in is implementation- defined. If you want a way to transparently write and read data, use ... <drum-roll> ... binary streams and the corresponding functions And fgets has done a good job in the past, and I suspect that won't change soon. Just use it for the purpose it's designed for and everything's fine. For other purposes use other suitable library functions. And if there is none available, write one. That's not the point. The point is that if fgets had the semantics you propose, it would no longer be the library function for reading lines from text streams, and a need for the_real_fgets() would arise. It's not so damn hard to write five lines of C code to handle broken text files, put it in your toolbox and be happy for the rest of your life. [1] I could well live with a fgets implementation that invokes undefined behaviour if it hits a NUL chartacter in a text stream. Regards [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Is C99 the final C?
Top