1FSEEK(3P) POSIX Programmer's Manual FSEEK(3P)
2
3
4
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
12 fseek, fseeko — reposition a file-position indicator in a stream
13
15 #include <stdio.h>
16
17 int fseek(FILE *stream, long offset, int whence);
18 int fseeko(FILE *stream, off_t offset, int whence);
19
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‐2017
24 defers to the ISO C standard.
25
26 The fseek() function shall set the file-position indicator for the
27 stream pointed to by stream. If a read or write error occurs, the
28 error indicator for the stream shall be set and fseek() fails.
29
30 The new position, measured in bytes from the beginning of the file,
31 shall be obtained by adding offset to the position specified by whence.
32 The specified point is the beginning of the file for SEEK_SET, the cur‐
33 rent value of the file-position indicator for SEEK_CUR, or end-of-file
34 for SEEK_END.
35
36 If the stream is to be used with wide-character input/output functions,
37 the application shall ensure that offset is either 0 or a value
38 returned by an earlier call to ftell() on the same stream and whence is
39 SEEK_SET.
40
41 A successful call to fseek() shall clear the end-of-file indicator for
42 the stream and undo any effects of ungetc() and ungetwc() on the same
43 stream. After an fseek() call, the next operation on an update stream
44 may be either input or output.
45
46 If the most recent operation, other than ftell(), on a given stream is
47 fflush(), the file offset in the underlying open file description shall
48 be adjusted to reflect the location specified by fseek().
49
50 The fseek() function shall allow the file-position indicator to be set
51 beyond the end of existing data in the file. If data is later written
52 at this point, subsequent reads of data in the gap shall return bytes
53 with the value 0 until data is actually written into the gap.
54
55 The behavior of fseek() on devices which are incapable of seeking is
56 implementation-defined. The value of the file offset associated with
57 such a device is undefined.
58
59 If the stream is writable and buffered data had not been written to the
60 underlying file, fseek() shall cause the unwritten data to be written
61 to the file and shall mark the last data modification and last file
62 status change timestamps of the file for update.
63
64 In a locale with state-dependent encoding, whether fseek() restores the
65 stream's shift state is implementation-defined.
66
67 The fseeko() function shall be equivalent to the fseek() function
68 except that the offset argument is of type off_t.
69
71 The fseek() and fseeko() functions shall return 0 if they succeed.
72
73 Otherwise, they shall return -1 and set errno to indicate the error.
74
76 The fseek() and fseeko() functions shall fail if, either the stream is
77 unbuffered or the stream's buffer needed to be flushed, and the call to
78 fseek() or fseeko() causes an underlying lseek() or write() to be
79 invoked, and:
80
81 EAGAIN The O_NONBLOCK flag is set for the file descriptor and the
82 thread would be delayed in the write operation.
83
84 EBADF The file descriptor underlying the stream file is not open for
85 writing or the stream's buffer needed to be flushed and the file
86 is not open.
87
88 EFBIG An attempt was made to write a file that exceeds the maximum
89 file size.
90
91 EFBIG An attempt was made to write a file that exceeds the file size
92 limit of the process.
93
94 EFBIG The file is a regular file and an attempt was made to write at
95 or beyond the offset maximum associated with the corresponding
96 stream.
97
98 EINTR The write operation was terminated due to the receipt of a sig‐
99 nal, and no data was transferred.
100
101 EINVAL The whence argument is invalid. The resulting file-position
102 indicator would be set to a negative value.
103
104 EIO A physical I/O error has occurred, or the process is a member of
105 a background process group attempting to perform a write() to
106 its controlling terminal, TOSTOP is set, the calling thread is
107 not blocking SIGTTOU, the process is not ignoring SIGTTOU, and
108 the process group of the process is orphaned. This error may
109 also be returned under implementation-defined conditions.
110
111 ENOSPC There was no free space remaining on the device containing the
112 file.
113
114 EOVERFLOW
115 For fseek(), the resulting file offset would be a value which
116 cannot be represented correctly in an object of type long.
117
118 EOVERFLOW
119 For fseeko(), the resulting file offset would be a value which
120 cannot be represented correctly in an object of type off_t.
121
122 EPIPE An attempt was made to write to a pipe or FIFO that is not open
123 for reading by any process; a SIGPIPE signal shall also be sent
124 to the thread.
125
126 ESPIPE The file descriptor underlying stream is associated with a pipe,
127 FIFO, or socket.
128
129 The fseek() and fseeko() functions may fail if:
130
131 ENXIO A request was made of a nonexistent device, or the request was
132 outside the capabilities of the device.
133
134 The following sections are informative.
135
137 None.
138
140 None.
141
143 None.
144
146 None.
147
149 Section 2.5, Standard I/O Streams, fopen(), fsetpos(), ftell(), getr‐
150 limit(), lseek(), rewind(), ulimit(), ungetc(), write()
151
152 The Base Definitions volume of POSIX.1‐2017, <stdio.h>
153
155 Portions of this text are reprinted and reproduced in electronic form
156 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
157 table Operating System Interface (POSIX), The Open Group Base Specifi‐
158 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
159 Electrical and Electronics Engineers, Inc and The Open Group. In the
160 event of any discrepancy between this version and the original IEEE and
161 The Open Group Standard, the original IEEE and The Open Group Standard
162 is the referee document. The original Standard can be obtained online
163 at http://www.opengroup.org/unix/online.html .
164
165 Any typographical or formatting errors that appear in this page are
166 most likely to have been introduced during the conversion of the source
167 files to man page format. To report such errors, see https://www.ker‐
168 nel.org/doc/man-pages/reporting_bugs.html .
169
170
171
172IEEE/The Open Group 2017 FSEEK(3P)