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