xref: /original-bsd/lib/libc/stdio/fseek.3 (revision c3e32dec)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" %sccs.include.redist.man%
9.\"
10.\"     @(#)fseek.3	8.1 (Berkeley) 06/04/93
11.\"
12.Dd
13.Dt FSEEK 3
14.Os
15.Sh NAME
16.Nm fgetpos ,
17.Nm fseek ,
18.Nm fsetpos ,
19.Nm ftell ,
20.Nm rewind
21.Nd reposition a stream
22.Sh SYNOPSIS
23.Fd #include <stdio.h>
24.Ft int
25.Fn fseek "FILE *stream" "long offset" "int whence"
26.Ft long
27.Fn ftell "FILE *stream"
28.Ft void
29.Fn rewind "FILE *stream"
30.Ft int
31.Fn fgetpos "FILE *stream" "fpos_t *pos"
32.Ft int
33.Fn fsetpos "FILE *stream" "fpos_t *pos"
34.Sh DESCRIPTION
35The
36.Fn fseek
37function sets the file position indicator for the stream pointed
38to by
39.Fa stream .
40The new position, measured in bytes, is obtained by adding
41.Fa offset
42bytes to the position specified by
43.Fa whence .
44If
45.Fa whence
46is set to
47.Dv SEEK_SET ,
48.Dv SEEK_CUR ,
49or
50.Dv SEEK_END ,
51the offset is relative to the
52start of the file, the current position indicator, or end-of-file,
53respectively.
54A successful call to the
55.Fn fseek
56function clears the end-of-file indicator for the stream and undoes
57any effects of the
58.Xr ungetc 3
59function on the same stream.
60.Pp
61The
62.Fn ftell
63function
64obtains the current value of the file position indicator for the
65stream pointed to by
66.Fa stream .
67.Pp
68The
69.Fn rewind
70function sets the file position indicator for the stream pointed
71to by
72.Fa stream
73to the beginning of the file.
74It is equivalent to:
75.Pp
76.Dl (void)fseek(stream, 0L, SEEK_SET)
77.Pp
78except that the error indicator for the stream is also cleared
79(see
80.Xr clearerr 3 ) .
81.Pp
82The
83.Fn fgetpos
84and
85.Fn fsetpos
86functions
87are alternate interfaces equivalent to
88.Fn ftell
89and
90.Fn fseek
91(with whence set to
92.Dv SEEK_SET
93), setting and storing the current value of
94the file offset into or from the object referenced by
95.Fa pos .
96On some
97.Pq non- Ns Tn UNIX
98systems an
99.Dq Fa fpos_t
100object may be a complex object
101and these routines may be the only way to portably reposition a text stream.
102.Sh RETURN VALUES
103The
104.Fn rewind
105function
106returns no value.
107Upon successful completion,
108.Fn fgetpos ,
109.Fn fseek ,
110.Fn fsetpos
111return 0,
112and
113.Fn ftell
114returns the current offset.
115Otherwise, \-1 is returned and the global variable errno is set to
116indicate the error.
117.Sh ERRORS
118.Bl -tag -width [EINVAL]
119.It Bq Er EBADF
120The
121.Fa stream
122specified
123is not a seekable stream.
124.It Bq Er EINVAL
125The
126.Fa whence
127argument to
128.Fn fseek
129was not
130.Dv SEEK_SET ,
131.Dv SEEK_END ,
132or
133.Dv SEEK_CUR .
134.El
135.Pp
136The function
137.Fn fgetpos ,
138.Fn fseek ,
139.Fn fsetpos ,
140and
141.Fn ftell
142may also fail and set
143.Va errno
144for any of the errors specified for the routines
145.Xr fflush 3 ,
146.Xr fstat 2 ,
147.Xr lseek 2 ,
148and
149.Xr malloc 3 .
150.Sh SEE ALSO
151.Xr lseek 2
152.Sh STANDARDS
153The
154.Fn fgetpos ,
155.Fn fsetpos ,
156.Fn fseek ,
157.Fn ftell ,
158and
159.Fn rewind
160functions
161conform to
162.St -ansiC .
163