1FGETS(3P)                  POSIX Programmer's Manual                 FGETS(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       fgets — get a string from a stream
13

SYNOPSIS

15       #include <stdio.h>
16
17       char *fgets(char *restrict s, int n, FILE *restrict stream);
18

DESCRIPTION

20       The functionality described on this reference page is aligned with  the
21       ISO C  standard.  Any  conflict between the requirements described here
22       and the ISO C standard is unintentional. This  volume  of  POSIX.1‐2017
23       defers to the ISO C standard.
24
25       The  fgets()  function  shall  read  bytes  from  stream into the array
26       pointed to by s until n-1 bytes are read, or a <newline>  is  read  and
27       transferred  to  s,  or an end-of-file condition is encountered. A null
28       byte shall be written immediately after the last  byte  read  into  the
29       array.   If  the  end-of-file condition is encountered before any bytes
30       are read, the contents of the array  pointed  to  by  s  shall  not  be
31       changed.
32
33       The  fgets()  function  may  mark the last data access timestamp of the
34       file associated with stream for update. The last data access  timestamp
35       shall  be  marked  for  update  by  the  first  successful execution of
36       fgetc(), fgets(), fread(),  fscanf(),  getc(),  getchar(),  getdelim(),
37       getline(),  gets(),  or scanf() using stream that returns data not sup‐
38       plied by a prior call to ungetc().
39

RETURN VALUE

41       Upon successful completion, fgets() shall return s.  If the  stream  is
42       at  end-of-file,  the end-of-file indicator for the stream shall be set
43       and fgets() shall return a null pointer.  If a read error  occurs,  the
44       error  indicator  for  the  stream shall be set, fgets() shall return a
45       null pointer, and shall set errno to indicate the error.
46

ERRORS

48       Refer to fgetc().
49
50       The following sections are informative.
51

EXAMPLES

53   Reading Input
54       The following example uses fgets() to read lines of input.  It  assumes
55       that  the file it is reading is a text file and that lines in this text
56       file are no longer than 16384 (or {LINE_MAX} if it is less  than  16384
57       on  the  implementation where it is running) bytes long. (Note that the
58       standard utilities have no line length limit  if  sysconf(_SC_LINE_MAX)
59       returns   -1   without   setting  errno.   This  example  assumes  that
60       sysconf(_SC_LINE_MAX) will not fail.)
61
62
63           #include <limits.h>
64           #include <stdio.h>
65           #include <unistd.h>
66           #define MYLIMIT 16384
67
68           char *line;
69           int line_max;
70           if (LINE_MAX >= MYLIMIT) {
71               // Use maximum line size of MYLIMIT. If LINE_MAX is
72               // bigger than our limit, sysconf() cannot report a
73               // smaller limit.
74               line_max = MYLIMIT;
75           } else {
76               long limit = sysconf(_SC_LINE_MAX);
77               line_max = (limit < 0 || limit > MYLIMIT) ? MYLIMIT : (int)limit;
78           }
79
80           // line_max + 1 leaves room for the null byte added by fgets().
81           line = malloc(line_max + 1);
82           if (line == NULL) {
83               // out of space
84               ...
85               return error;
86           }
87
88           while (fgets(line, line_max + 1, fp) != NULL) {
89               // Verify that a full line has been read ...
90               // If not, report an error or prepare to treat the
91               // next time through the loop as a read of a
92               // continuation of the current line.
93               ...
94               // Process line ...
95               ...
96           }
97           free(line);
98           ...
99

APPLICATION USAGE

101       None.
102

RATIONALE

104       None.
105

FUTURE DIRECTIONS

107       None.
108

SEE ALSO

110       Section 2.5, Standard I/O Streams, fgetc(), fopen(), fread(), fscanf(),
111       getc(), getchar(), getdelim(), gets(), ungetc()
112
113       The Base Definitions volume of POSIX.1‐2017, <stdio.h>
114
116       Portions  of  this text are reprinted and reproduced in electronic form
117       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
118       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
119       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
120       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
121       event of any discrepancy between this version and the original IEEE and
122       The  Open Group Standard, the original IEEE and The Open Group Standard
123       is the referee document. The original Standard can be  obtained  online
124       at http://www.opengroup.org/unix/online.html .
125
126       Any  typographical  or  formatting  errors that appear in this page are
127       most likely to have been introduced during the conversion of the source
128       files  to  man page format. To report such errors, see https://www.ker
129       nel.org/doc/man-pages/reporting_bugs.html .
130
131
132
133IEEE/The Open Group                  2017                            FGETS(3P)
Impressum