blob: 36107c350a69c8787a5e4605d7355e1bc6691c67 [file] [log] [blame]
<html lang="en">
<head>
<title>File Position - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="I_002fO-Concepts.html#I_002fO-Concepts" title="I/O Concepts">
<link rel="prev" href="Streams-and-File-Descriptors.html#Streams-and-File-Descriptors" title="Streams and File Descriptors">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="File-Position"></a>
<p>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Streams-and-File-Descriptors.html#Streams-and-File-Descriptors">Streams and File Descriptors</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="I_002fO-Concepts.html#I_002fO-Concepts">I/O Concepts</a>
<hr>
</div>
<h4 class="subsection">11.1.2 File Position</h4>
<p>One of the attributes of an open file is its <dfn>file position</dfn> that
keeps track of where in the file the next character is to be read or
written. In the GNU system, and all POSIX.1 systems, the file position
is simply an integer representing the number of bytes from the beginning
of the file.
<p>The file position is normally set to the beginning of the file when it
is opened, and each time a character is read or written, the file
position is incremented. In other words, access to the file is normally
<dfn>sequential</dfn>.
<a name="index-file-position-899"></a><a name="index-sequential_002daccess-files-900"></a>
Ordinary files permit read or write operations at any position within
the file. Some other kinds of files may also permit this. Files which
do permit this are sometimes referred to as <dfn>random-access</dfn> files.
You can change the file position using the <code>fseek</code> function on a
stream (see <a href="File-Positioning.html#File-Positioning">File Positioning</a>) or the <code>lseek</code> function on a file
descriptor (see <a href="I_002fO-Primitives.html#I_002fO-Primitives">I/O Primitives</a>). If you try to change the file
position on a file that doesn't support random access, you get the
<code>ESPIPE</code> error.
<a name="index-random_002daccess-files-901"></a>
Streams and descriptors that are opened for <dfn>append access</dfn> are
treated specially for output: output to such files is <em>always</em>
appended sequentially to the <em>end</em> of the file, regardless of the
file position. However, the file position is still used to control where in
the file reading is done.
<a name="index-append_002daccess-files-902"></a>
If you think about it, you'll realize that several programs can read a
given file at the same time. In order for each program to be able to
read the file at its own pace, each program must have its own file
pointer, which is not affected by anything the other programs do.
<p>In fact, each opening of a file creates a separate file position.
Thus, if you open a file twice even in the same program, you get two
streams or descriptors with independent file positions.
<p>By contrast, if you open a descriptor and then duplicate it to get
another descriptor, these two descriptors share the same file position:
changing the file position of one descriptor will affect the other.
</body></html>