From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/io%2Ffgets.html | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 devdocs/c/io%2Ffgets.html (limited to 'devdocs/c/io%2Ffgets.html') diff --git a/devdocs/c/io%2Ffgets.html b/devdocs/c/io%2Ffgets.html new file mode 100644 index 00000000..c6178a7d --- /dev/null +++ b/devdocs/c/io%2Ffgets.html @@ -0,0 +1,71 @@ +

fgets

Defined in header <stdio.h>
char *fgets( char          *str, int count, FILE          *stream );
+
(until C99)
char *fgets( char *restrict str, int count, FILE *restrict stream );
+
(since C99)

Reads at most count - 1 characters from the given file stream and stores them in the character array pointed to by str. Parsing stops if a newline character is found, in which case str will contain that newline character, or if end-of-file occurs. If bytes are read and no errors occur, writes a null character at the position immediately after the last character written to str.

+

Parameters

+ + + +
str - pointer to an element of a char array
count - maximum number of characters to write (typically the length of str)
stream - file stream to read the data from

Return value

str on success, null pointer on failure.

+

If the end-of-file condition is encountered, sets the eof indicator on stream (see feof()). This is only a failure if it causes no bytes to be read, in which case a null pointer is returned and the contents of the array pointed to by str are not altered (i.e. the first byte is not overwritten with a null character).

+

If the failure has been caused by some other error, sets the error indicator (see ferror()) on stream. The contents of the array pointed to by str are indeterminate (it may not even be null-terminated).

+

Notes

POSIX additionally requires that fgets sets errno if a read error occurs.

+

Although the standard specification is unclear in the cases where count<=1, common implementations do

+

Example

#include <stdio.h>
+#include <stdlib.h>
+ 
+int main(void)
+{
+    FILE* tmpf = tmpfile();
+    fputs("Alan Turing\n", tmpf);
+    fputs("John von Neumann\n", tmpf);
+    fputs("Alonzo Church\n", tmpf);
+ 
+    rewind(tmpf);
+ 
+    char buf[8];
+    while (fgets(buf, sizeof buf, tmpf) != NULL)
+          printf("\"%s\"\n", buf);
+ 
+    if (feof(tmpf))
+       puts("End of file reached");
+}

Output:

+
"Alan Tu"
+"ring
+"
+"John vo"
+"n Neuma"
+"nn
+"
+"Alonzo "
+"Church
+"
+End of file reached

References

See also

+ + + + +
+
(C11)(C11)(C11)
reads formatted input from stdin, a file stream or a buffer
(function)
+
(removed in C11)(C11)
reads a character string from stdin
(function)
writes a character string to a file stream
(function)
+
(dynamic memory TR)
read from a stream into an automatically resized buffer until delimiter/end of line
(function)
C++ documentation for fgets
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/io/fgets +

+
-- cgit v1.2.3