⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib_file.html

📁 ST20 Embedded Toolset R2.0.5用于开发基于ST20芯片机顶盒软件的开发平台,2.0.5版本,国内找不到的.在国外论坛上花了N天才找到!
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>Files and Streams</TITLE></HEAD><BODY><H1><A NAME="Files and Streams">Files and Streams</A></H1><HR><P><B><A HREF="#Text and Binary Streams">Text and Binary Streams</A>&#183; <A HREF="#Byte and Wide Streams">Byte and Wide Streams</A>&#183; <A HREF="#Controlling Streams">Controlling Streams</A>&#183; <A HREF="#Stream States">Stream States</A></B></P><HR><P>A program communicates with the target environment by reading and writing<B><A NAME="files">files</A></B> (ordered sequences of bytes). A file canbe, for example, a data set that you can read and write repeatedly(such as a disk file), a stream of bytes generated by a program (suchas a pipeline), or a stream of bytes received from or sent to a peripheraldevice (such as the keyboard or display). The latter two are<B><A NAME="interactive files">interactive files</A></B>.Files are typically the principal means by which to interact with aprogram.</P><P>You manipulate all these kinds of files in much the same way-- by calling library functions. You include the standard header<CODE>&lt;stdio.h&gt;</CODE> to declare most of these functions.</P><P>Before you can perform many of the operations on a file, thefile must be<B><A NAME="file open">opened</A></B>.Opening a file associates it with a<B><A NAME="stream">stream</A></B>, a data structure withinthe Standard C library that glosses over many differencesamong files of various kinds.The library maintains the state of each stream in an object of type<A HREF="stdio.html#FILE"><CODE>FILE</CODE></A>.</P><P>The target environment opens three files prior to<A HREF="lib_over.html#program startup">program startup</A>.You can open a file by calling the library function<A HREF="stdio.html#fopen"><CODE>fopen</CODE></A> withtwo arguments. The first argument is a<A HREF="lib_over.html#filename">filename</A>, a<A HREF="lib_over.html#multibyte string">multibyte string</A>that the target environment uses to identify which file youwant to read or write. The second argument is a<A HREF="lib_over.html#C string">C string</A> that specifies:</P><UL><LI>whether you intend to read data from the file or write datato it or both</LI><LI>whether you intend to generate new contents for the file (orcreate a file if it did not previously exist) or leave the existingcontents in place</LI><LI>whether writes to a file can alter existing contents or shouldonly append bytes at the end of the file</LI><LI>whether you want to manipulate a<A HREF="#text stream">text stream</A> or a<A HREF="#binary stream">binary stream</A></LI></UL><P>Once the file is successfully opened, you can then determinewhether the stream is<B><A NAME="byte oriented">byte oriented</A></B> (a<A HREF="#byte stream">byte stream</A>) or<B><A NAME="wide oriented">wide oriented</A></B> (a<A HREF="#wide stream">wide stream</A>).Wide-oriented streams are supported only with<A HREF="lib_over.html#Amendment 1">Amendment 1</A>.A stream is initially<B><A NAME="unbound stream">unbound</A></B>.Calling certain functions to operate on the stream makes it byte oriented,while certain other functions make it wide oriented. Once established,a stream maintains its orientation until it is closed by a call to<A HREF="stdio.html#fclose"><CODE>fclose</CODE></A> or<A HREF="stdio.html#freopen"><CODE>freopen</CODE></A>.</P><H2><A NAME="Text and Binary Streams">Text and Binary Streams</A></H2><P>A<B><A NAME="text stream">text stream</A></B> consists of one or more<B><A NAME="text lines">lines</A></B> of textthat can be written to a text-oriented display so that they canbe read. When reading from a text stream, the program reads an<CODE><I>NL</I></CODE> (newline) at the end of each line.When writing to a text stream, the program writes an<CODE><I>NL</I></CODE> to signal the end of a line. To matchdiffering conventions among target environments for representing textin files, the library functions can alter the number and representationsof characters transmitted between the program and a text stream.</P><P>Thus, positioning within a text stream is limited.You can obtain the current<A HREF="#file-position indicator">file-position indicator</A>by calling <CODE><A HREF="stdio.html#fgetpos">fgetpos</A></CODE> or<CODE><A HREF="stdio.html#ftell">ftell</A></CODE>.You can position a text stream at a position obtained this way,or at the beginning or end of the stream, by calling<CODE><A HREF="stdio.html#fsetpos">fsetpos</A></CODE> or<CODE><A HREF="stdio.html#fseek">fseek</A></CODE>.Any other change of position might well be not supported.</P><P>For maximum portability, the program should not write:</P><UL><LI>empty files</LI><LI><CODE><I>space</I></CODE> characters at the end of a line</LI><LI>partial lines (by omitting the <CODE><I>NL</I></CODE>at the end of a file)</LI><LI>characters other than the printable characters,<CODE><I>NL</I></CODE>, and <CODE><I>HT</I></CODE> (horizontal tab)</LI></UL><P>If you follow these rules, the sequence of characters you readfrom a text stream (either as byte or multibyte characters)will match the sequence of characters you wrote to the text streamwhen you created the file. Otherwise, the library functions can removea file you create if the file is empty when you close it. Or theycan alter or delete characters you write to the file.</P><P>A<B><A NAME="binary stream">binary stream</A></B> consists ofone or more bytes of arbitrary information.You can write the value stored in an arbitrary objectto a (byte-oriented) binary stream and read exactly what was storedin the object when you wrote it. The library functions do not alterthe bytes you transmit between the program and a binary stream. Theycan, however, append an arbitrary number of null bytes to the filethat you write with a binary stream. The program must deal with theseadditional null bytes at the end of any binary stream.</P><P>Thus, positioning within a binary stream is well defined,except for positioning relative to the end of the stream.You can obtain and alter the current<A HREF="#file-position indicator">file-position indicator</A>the same as for a <A HREF="#text stream">text stream</A>.Moreover, the offsets used by<CODE><A HREF="stdio.html#ftell">ftell</A></CODE> and<CODE><A HREF="stdio.html#fseek">fseek</A></CODE>count bytes from the beginning of the stream (which is byte zero),so integer arithmetic on these offsets yields predictable results.</P><H2><A NAME="Byte and Wide Streams">Byte and Wide Streams</A></H2><P>A<B><A NAME="byte stream">byte stream</A></B>treats a file as a sequence of bytes. Within the program,the stream looks like the same sequence of bytes, exceptfor the possible alterations described above.</P><P>By contrast, a<B><A NAME="wide stream">wide stream</A></B> treats a file as a sequence of<B><A NAME="generalized multibyte characters">generalized multibyte characters</A></B>,which can have a broad range of encoding rules.(Text and binary files are still read and written as described above.)Within the program, the stream looks like the corresponding sequence of<A HREF="charset.html#Wide-Character Encoding">wide characters</A>.Conversions between the two representations occurwithin the Standard C library. The conversion rules can, in principle,be altered by a call to<A HREF="locale.html#setlocale"><CODE>setlocale</CODE></A>that alters the category<A HREF="locale.html#LC_CTYPE"><CODE>LC_CTYPE</CODE></A>.Each wide stream determines its conversion rulesat the time it becomes wide oriented, and retainsthese rules even if the category<A HREF="locale.html#LC_CTYPE"><CODE>LC_CTYPE</CODE></A>subsequently changes.</P><P>Positioning within a wide stream suffers the same limitations as for<A HREF="#text stream">text streams</A>. Moreover, the<A HREF="#file-position indicator">file-position indicator</A>may well have to deal with a<A HREF="charset.html#state-dependent encoding">state-dependent encoding</A>.Typically, it includes both a byte offset within the streamand an object of type<CODE><A HREF="wchar.html#mbstate_t">mbstate_t</A></CODE>. Thus, the onlyreliable way to obtain a file position within a wide stream is by calling<CODE><A HREF="stdio.html#fgetpos">fgetpos</A></CODE>,and the only reliable way to restore a positionobtained this way is by calling<CODE><A HREF="stdio.html#fsetpos">fsetpos</A></CODE>.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -