📄 171-174.html
字号:
</form>
<!-- LEFT NAV SEARCH END -->
</td>
<!-- PUB PARTNERS END -->
<!-- END LEFT NAV -->
<td rowspan="8" align="right" valign="top"><img src="/images/iswbls.gif" width=1 height=400 alt="" border="0"></td>
<td><img src="/images/white.gif" width="5" height="1" alt="" border="0"></td>
<!-- end of ITK left NAV -->
<!-- begin main content -->
<td width="100%" valign="top" align="left">
<!-- END SUB HEADER -->
<!--Begin Content Column -->
<FONT FACE="Arial,Helvetica" SIZE="-1">
To access the contents, click the chapter and section titles.
</FONT>
<P>
<B>Essential Windows CE Application Programming</B>
<FONT SIZE="-1">
<BR>
<I>(Publisher: John Wiley & Sons, Inc.)</I>
<BR>
Author(s): Robert Burdick
<BR>
ISBN: 0471327476
<BR>
Publication Date: 03/01/99
</FONT>
<P>
<form name="Search" method="GET" action="http://search.earthweb.com/search97/search_redir.cgi">
<INPUT TYPE="hidden" NAME="Action" VALUE="Search">
<INPUT TYPE="hidden" NAME="SearchPage" VALUE="http://search.earthweb.com/search97/samples/forms/srchdemo.htm">
<INPUT TYPE="hidden" NAME="Collection" VALUE="ITK">
<INPUT TYPE="hidden" NAME="ResultTemplate" VALUE="itk-simple-intrabook.hts">
<INPUT TYPE="hidden" NAME="ViewTemplate" VALUE="view.hts">
<font face="arial, helvetica" size=2><b>Search this book:</b></font><br>
<INPUT NAME="queryText" size=50 VALUE=""> <input type="submit" name="submitbutton" value="Go!">
<INPUT type=hidden NAME="section_on" VALUE="on">
<INPUT type=hidden NAME="section" VALUE="http://www.itknowledge.com/reference/standard/0471327476/">
</form>
<!-- Empty Reference Subhead -->
<!--ISBN=0471327476//-->
<!--TITLE=Essential Windows CE Application Programming//-->
<!--AUTHOR=Robert Burdick//-->
<!--PUBLISHER=John Wiley & Sons, Inc.//-->
<!--IMPRINT=Wiley Computer Publishing//-->
<!--CHAPTER=6//-->
<!--PAGES=171-174//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="168-171.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="174-178.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading10"></A><FONT COLOR="#000077">Reading and Writing File Data</FONT></H3>
<P>File read and write operations are closely linked to the concept of the <I>file pointer</I>. A file pointer marks the current position in a given file. Read operations read data from the file’s current position. Write operations write data to the file at the position indicated by the file pointer.</P>
<P>Files also have an <I>end of file</I> marker. This marker indicates the last byte of data in the file. As such, the end of file marker also determines the size of the file. As file write operations increase the size of a file, they move this end of file marker. Hence there is no such thing as writing past the end of a file: files grow to accommodate the data being written to them.</P>
<P>Files access can be either <I>sequential</I> or <I>random</I>. Sequential access means that data is read from the file in order. Random access means that data can be read from the file in any order as determined by the application reading the file. For random access to be possible, there must be a way for applications to manually set the file pointer without requiring read or write operations to occur. We will introduce such functions later in this chapter.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B><B>A<SMALL>SYNCHRONOUS</SMALL> F<SMALL>ILE</SMALL> A<SMALL>CCESS</SMALL></B>
<P><B>Under Windows NT, file access operations can be synchronous or asynchronous. Windows CE however does <I>not</I> support asynchronous access.</B><HR></FONT>
</BLOCKQUOTE>
</P>
<P><FONT SIZE="+1"><B>The <I>ReadFile</I> Function
</B></FONT></P>
<P>Data is read from a file using the <I>ReadFile</I> function:</P>
<!-- CODE SNIP //-->
<PRE>
ReadFile(hFile, lpBuffer, nNumberOfBytesToRead,
lpNumberOfBytesRead, lpOverlapped);
</PRE>
<!-- END CODE SNIP //-->
<P><I>hFile</I> is the handle of the open file from which the data is to be read. <I>lpBuffer</I> is a pointer to the data buffer which receives the data. <I>nNumberOfBytesToRead</I> specifies the number of bytes of data to read from the file. <I>lpNumberOfBytesRead</I> is a pointer to a DWORD used by <I>ReadFile</I> to return the actual number of bytes of data read. As Windows CE does not allow files to be created with the FILE_FLAG_OVERLAPPED flag, <I>lpOverlapped</I> is not used. <I>lpOverlapped</I> should be set to NULL.</P>
<P><I>ReadFile</I> returns TRUE if the operation is successful, and FALSE if the operation fails. An application can get additional error information in this case by calling <I>GetLastError</I>.</P>
<P><I>ReadFile</I> returns once the number of bytes specified in <I>nNumberOfBytesToRead</I> have been read, or when an error occurs. If an application specifies that more bytes be read than the file actually contains, <I>ReadFile</I> will simply read as many as it can and return the actual number of bytes read in <I>lpNumberOfBytesRead</I>.</P>
<P><FONT SIZE="+1"><B>Random Access Files</B></FONT></P>
<P>The <I>ReadFile</I> function advances the file pointer <I>lpNumberOfBytesRead</I>. This accounts for the default sequential nature of file access. To implement random file access, your applications must be able to control the position of the file pointer manually. This can be done using the <I>SetFilePointer</I> function. <I>SetFilePointer</I> can be used to move a file pointer by specifying a 64-bit number representing the number of bytes the pointer is to be moved.</P>
<P>The syntax of <I>SetFilePointer</I> is:</P>
<!-- CODE SNIP //-->
<PRE>
SetFilePointer(hFile, lDistanceToMove,
lpDistanceToMoveHigh, dwMoveMethod);
</PRE>
<!-- END CODE SNIP //-->
<P><I>hFile</I> is the handle of the open file whose pointer is to be moved. <I>lDistanceToMove</I> specifies the low order word of the number of bytes to move the file pointer. This value can be negative, in which case the file pointer is moved backward. <I>lpDistanceToMoveHigh</I> is a pointer to the high order word of the number of bytes to move the file pointer. This parameter is also used by <I>SetFilePointer</I> to return the high order word of the new file pointer position.</P>
<P>The <I>dwMoveMethod</I> parameter indicates the starting point of the move operation. It can be one of the following three values:</P>
<DL>
<DD><B>FILE_BEGIN.</B> The starting point is the beginning of the file. In this case, the distance to move is interpreted as the unsigned pointer location.
<DD><B>FILE_CURRENT.</B> The starting point is the current file pointer position.
<DD><B>FILE_END.</B> The starting point is the end of file position.
</DL>
<P>If the function succeeds, it returns the low order word of the new file pointer position. If <I>lpDistanceToMoveHigh</I> was not NULL, this parameter will return the high order word of the new position. If <I>SetFilePointer</I> fails, the return value is –1 and <I>lpDistanceToMoveHigh</I> is NULL.</P>
<P>Random access of Windows CE files can therefore be accomplished by first specifying FILE_FLAG_RANDOM_ACCESS as one of the <I>dwFlagsAndAttributes</I> values when creating or opening the file with <I>CreateFile</I>. <I>SetFilePointer</I> is then called to manually position the file pointer for read and write operations.</P>
<P>An application may also want to change the position of the end of file marker of a particular file. This is done using the <I>SetEndOfFile</I> function:</P>
<!-- CODE SNIP //-->
<PRE>
SetEndOfFile(hFile);
</PRE>
<!-- END CODE SNIP //-->
<P>This functions moves the end of file marker of the file specified by the file handle <I>hFile</I> to the current file pointer position of that file. If successful, this function returns TRUE. Otherwise it returns FALSE.</P>
<P>For example, to move the end of file marker to the beginning of a file, an application could do this:</P>
<!-- CODE SNIP //-->
<PRE>
//Set file pointer to beginning of file
SetFilePointer(hMyFile, 0, 0, FILE_BEGIN);
//Now set end of file marker
SetEndOfFile(hMyFile);
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="168-171.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="174-178.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<!-- all of the reference materials (books) have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->
<!-- BEGIN SUB FOOTER -->
<br><br>
</TD>
</TR>
</TABLE>
<table width="640" border=0 cellpadding=0 cellspacing=0>
<tr>
<td align="left" width=135><img src="/images/white.gif" width=100 height="1" alt="" border="0"></td>
<!-- END SUB FOOTER -->
<!-- all of the books have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->
<!-- FOOTER -->
<td width="515" align="left" bgcolor="#FFFFFF">
<font face="arial, helvetica" size="1"><b><a href="/products.html"><font color="#006666">Products</font></a> | <a href="/contactus.html"><font color="#006666">Contact Us</font></a> | <a href="/aboutus.html"><font color="#006666">About Us</font></a> | <a href="http://www.earthweb.com/corporate/privacy.html" target="_blank"><font color="#006666">Privacy</font></a> | <a href="http://www.itmarketer.com/" target="_blank"><font color="#006666">Ad Info</font></a> | <a href="/"><font color="#006666">Home</font></a></b>
<br><br>
Use of this site is subject to certain <a href="/agreement.html">Terms & Conditions</a>, <a href="/copyright.html">Copyright © 1996-1999 EarthWeb Inc.</a><br>
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.</font><p>
</td>
</tr>
</table>
</BODY>
</HTML>
<!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -