98 lines
7.4 KiB
HTML
98 lines
7.4 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Function FILE-POSITION</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="f_file_l.htm">
|
|
<LINK REL=UP HREF="c_stream.htm">
|
|
<LINK REL=NEXT HREF="f_file_s.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="f_file_l.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_stream.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="f_file_s.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="file-position"><I>Function</I> <B>FILE-POSITION</B></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>file-position</B> <I>stream</I> => <I>position</I><P>
|
|
|
|
<B>file-position</B> <I>stream position-spec</I> => <I>success-p</I><P>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>stream</I>---a <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A>. <P>
|
|
<I>position-spec</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#file_position_designator"><I>file position designator</I></A>. <P>
|
|
<I>position</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#file_position"><I>file position</I></A> or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
|
|
<I>success-p</I>---a <A REL=DEFINITION HREF="26_glo_g.htm#generalized_boolean"><I>generalized boolean</I></A>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
Returns or changes the current position within a <I>stream</I>. <P>
|
|
When <I>position-spec</I> is not supplied, <A REL=DEFINITION HREF="#file-position"><B>file-position</B></A> returns the current <A REL=DEFINITION HREF="26_glo_f.htm#file_position"><I>file position</I></A> in the <I>stream</I>, or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> if this cannot be determined. <P>
|
|
When <I>position-spec</I> is supplied, the <A REL=DEFINITION HREF="26_glo_f.htm#file_position"><I>file position</I></A> in <I>stream</I> is set to that <A REL=DEFINITION HREF="26_glo_f.htm#file_position"><I>file position</I></A> (if possible). <A REL=DEFINITION HREF="#file-position"><B>file-position</B></A> returns <A REL=DEFINITION HREF="26_glo_t.htm#true"><I>true</I></A> if the repositioning is performed successfully, or <A REL=DEFINITION HREF="26_glo_f.htm#false"><I>false</I></A> if it is not. <P>
|
|
An <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integer</I></A> returned by <A REL=DEFINITION HREF="#file-position"><B>file-position</B></A> of one argument should be acceptable as <I>position-spec</I> for use with the same file. <P>
|
|
For a character file, performing a single <A REL=DEFINITION HREF="f_rd_cha.htm#read-char"><B>read-char</B></A> or <A REL=DEFINITION HREF="f_wr_cha.htm#write-char"><B>write-char</B></A> operation may cause the file position to be increased by more than 1 because of character-set translations (such as translating between the Common Lisp f#\Newline character and an external ASCII carriage-return/line-feed sequence) and other aspects of the implementation. For a binary file, every <A REL=DEFINITION HREF="f_rd_by.htm#read-byte"><B>read-byte</B></A> or <A REL=DEFINITION HREF="f_wr_by.htm#write-byte"><B>write-byte</B></A> operation increases the file position by 1. <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(defun tester ()
|
|
(let ((noticed '()) file-written)
|
|
(flet ((notice (x) (push x noticed) x))
|
|
(with-open-file (s "test.bin"
|
|
:element-type '(unsigned-byte 8)
|
|
:direction :output
|
|
:if-exists :error)
|
|
(notice (file-position s)) ;1
|
|
(write-byte 5 s)
|
|
(write-byte 6 s)
|
|
(let ((p (file-position s)))
|
|
(notice p) ;2
|
|
(notice (when p (file-position s (1- p))))) ;3
|
|
(write-byte 7 s)
|
|
(notice (file-position s)) ;4
|
|
(setq file-written (truename s)))
|
|
(with-open-file (s file-written
|
|
:element-type '(unsigned-byte 8)
|
|
:direction :input)
|
|
(notice (file-position s)) ;5
|
|
(let ((length (file-length s)))
|
|
(notice length) ;6
|
|
(when length
|
|
(dotimes (i length)
|
|
(notice (read-byte s)))))) ;7,...
|
|
(nreverse noticed))))
|
|
=> tester
|
|
(tester)
|
|
=> (0 2 T 2 0 2 5 7)
|
|
OR=> (0 2 NIL 3 0 3 5 6 7)
|
|
OR=> (NIL NIL NIL NIL NIL NIL)
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>Side Effects:</B><P>
|
|
<P>
|
|
When the <I>position-spec</I> argument is supplied, the <A REL=DEFINITION HREF="26_glo_f.htm#file_position"><I>file position</I></A> in the <I>stream</I> might be moved. <P>
|
|
<P><B>Affected By:</B><P>
|
|
<P>
|
|
The value returned by <A REL=DEFINITION HREF="#file-position"><B>file-position</B></A> increases monotonically as input or output operations are performed. <P>
|
|
<P><B>Exceptional Situations:</B><P>
|
|
<P>
|
|
If <I>position-spec</I> is supplied, but is too large or otherwise inappropriate, an error is signaled. <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="f_file_l.htm#file-length"><B>file-length</B></A>, <A REL=DEFINITION HREF="f_file_s.htm#file-string-length"><B>file-string-length</B></A>, <A REL=DEFINITION HREF="f_open.htm#open"><B>open</B></A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
Implementations that have character files represented as a sequence of records of bounded size might choose to encode the file position as, for example, <<<I>record-number</I>>>*<<<I>max-record-size</I>>>+<<<I>character-within-record</I>>>. This is a valid encoding because it increases monotonically as each character is read or written, though not necessarily by 1 at each step. An <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integer</I></A> might then be considered ``inappropriate'' as <I>position-spec</I> to <A REL=DEFINITION HREF="#file-position"><B>file-position</B></A> if, when decoded into record number and character number, it turned out that the supplied record was too short for the specified character number. <P>
|
|
<HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|