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
11

NAME

13       fgets — get a string from a stream
14

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

46       Refer to fgetc().
47
48       The following sections are informative.
49

EXAMPLES

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

APPLICATION USAGE

98       None.
99

RATIONALE

101       None.
102

FUTURE DIRECTIONS

104       None.
105

SEE ALSO

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