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

📄 vel18.htm

📁 简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel18.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="velp09.htm"><LINK REL="Previous" HREF="vel17.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel18.htm</H2><P ALIGN=LEFT><A HREF="vel17.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="velp09.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E132" >What You'll Learn</A><LI><A HREF="#E68E133" >Disk File Background</A><LI><A HREF="#E68E134" >Opening Files</A><LI><A HREF="#E68E135" >Clean Up with Close</A><LI><A HREF="#E68E136" >Writing to Files With Write</A><LI><A HREF="#E68E137" >Inputting from Files with Input#</A><LI><A HREF="#E68E138" >Line Input# Really Makes it Easy</A><LI><A HREF="#E68E139" >Homework</A><UL><LI><A HREF="#E69E122" >General Knowledge</A><LI><A HREF="#E69E123" >What's the Output?</A><LI><A HREF="#E69E124" >Find the Bug</A><LI><A HREF="#E69E125" >Write Code That...</A><LI><A HREF="#E69E126" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E26"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 9, Unit 18</B></FONT></CENTER></H1><BR><A NAME="E67E29"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Simple File I/O</B></FONT></CENTER></H2><BR><BR><A NAME="E68E132"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>Creating the disk file background<BR><BR><LI>Opening files<BR><BR><LI>Cleaning up with Close<BR><BR><LI>Writing to files with Write<BR><BR><LI>Inputting from files with Input#<BR><BR><LI>Making it really easy with Line Input#<BR><BR></UL><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: I/O</I> means input and output.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>This lesson explains how you can use Visual Basic code to manage disk file I/O. If you've collected data from the user and stored that data in variables and arrays, you can save the data to the disk for later retrieval. Also, you can access disk files from within Visual Basic for product inventory codes, amounts, customer balances, and whatever else your program needs from the long-term data file storage.<BR><P>As you master Visual Basic and upgrade to other Visual Basic products, you'll add additional controls to your Toolbox window. There are several database access controls that read and write the data you've put in databases using products such as Microsoft Access and Paradox. Even though these controls provide more power and ease than you can get by programming alone, you'll still need the fundamentals of disk access. This unit explains a little on the background of disk access and teaches some of the most important disk commands and functions that you need to work with data files.<BR><BR><A NAME="E68E133"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Disk File Background</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>A file is a collection of related data as well as programs that you buy and write, and documents from your word processor. Generally, you'll use Visual Basic to access data and text files stored on the disk.<BR><P>There are all kinds of files on your computer's disks. Every file is stored under a unique filename to its directory and disk drive. Therefore, there can't be two or more files with the same filename unless the files reside in different directories or on different disks.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>data file</I> holds data on the disk.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>This unit is concerned with data files. Data files can take on all kinds of formats. Generally, newcomers to Visual Basic should stick with data files that are textual in nature. Text files are readable by virtually any kind of program, and virtually any program can produce text files. Sometimes, text files are called ASCII files because text files consist of strings of ASCII characters.<BR><P>Before Visual Basic can access a file, you or the user will have to direct Visual Basic to the exact location on the exact disk where the file is stored. If your user is selecting a file, you'll want to use the file selection frame described in the previous unit to give the user the ability to change drives, directories, and filenames easily. When your program accesses a file that the user doesn't know about, your program will have to supply the drive, directory, and filename.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The project at the end of this lesson contains an application that combines the file dialog frame that you mastered in the previous unit with the file I/O commands and functions described here to build a complete file access and display program.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>This unit teaches you how to access text data files stored on the disk. You'll need to supply Visual Basic with the filename, directory, and disk drive of any file with which Visual Basic works.<BR><BR><A NAME="E68E134"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Opening Files</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Open statement opens files. Before Visual Basic can access a data file, Visual Basic has to open the file first. Think of the Open statement as doing for Visual Basic what an open file drawer does for you when you want to retrieve a file from a filing cabinet. The Open statement locates the file and makes the file available to Visual Basic.<BR><P>The Open statement performs various tasks such as locating a file, making sure that the file exists if needed, and creating some directory entries that manage the file while the file is open. A Visual Basic program always has to open a file, using Open, before the program can read or write data to the file.<BR><P>Here is the format of Open:<BR><BR><PRE><FONT COLOR="#000080">Open FileNameStr [For mode] As [#]FileNumber</FONT></PRE><P>The <I>FileNameStr</I> must be a string value or variable that holds a filename. The filename must reside on the default drive or directory unless you specify the full path to the file. Generally, you won't have easy access to the user's current Windows default drive and directory, so you'll almost always specify the full drive and pathname inside the <I>FileNameStr</I> portion of the Open statement.<BR><P>The <I>mode</I> must be a named value from Table 18.1. There are additional <I>mode</I> values, but this book won't cover the more advanced or the esoteric <I>mode</I> values. The <I>mode</I> tells Visual Basic exactly what your program expects to do with the file once Visual Basic opens the file.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 18.1. Possible </B><FONT COLOR="#FF8000"><B><I>mode</I></B></FONT><B> values for the </B><B>Open</B><B> statement.</B></FONT></CENTER><BR><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Mode</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Append</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Tells Visual Basic that your program needs to write to the end of the file if the file already exists. If the file doesn't exist, Visual Basic creates the file so that your program can write data to the file.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Input</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Tells Visual Basic that your program needs to read from the file. If the file doesn't exist, Visual Basic issues an error message. As long as you use a file selection frame properly, Visual Basic will never issue an error, because the file selection frame forces the user to select a file or cancel the selection operation.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Output</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Tells Visual Basic that your program needs to write to the file. If the file doesn't exist, Visual Basic creates the file. If the file does exist, Visual Basic first erases the existing file and creates a new one under the same name, thereby replacing the original one.</FONT></TABLE><P>The pound sign, #, is optional, although most Visual Basic programmers do specify the pound sign out of habit (some previous versions of the BASIC language required the pound sign). The <I>FileNumber</I> represents a number from 1 to 255 and associates the open file with that number. After you open a file successfully (assuming that there are no errors such as a disk drive door being left open), the rest of the program uses file I/O commands and functions to access the file. The file number stays with the file until you issue a Close command (see the next section) that releases the <I>FileNumber</I> and makes the number available to other files.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>As with all DOS and Windows file descriptions, you can specify the drive, directory, and filename using uppercase or lowercase characters.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>You can open more than one file simultaneously within a single program. Each command that accesses one of the files directs its activity towards a specific file using that file's <I>FileNumber</I>.<BR><P>The following Open statement creates and opens a data file on the disk drive and associates the file to the file number 1:<BR><BR><PRE><FONT COLOR="#000080">Open &quot;d:\data\myfile.dat&quot; For Output As #1</FONT></PRE><P>If you knew that the file already existed and you needed to add to the file, you could use the Append <I>mode</I> to add to the file after this Open statement:<BR><BR><PRE><FONT COLOR="#000080">Open &quot;d:\data\myfile.dat&quot; For Append As #1</FONT></PRE><P>One Visual Basic program can have more than one file open at the same time. There is an advanced FILES option in your computer's CONFIG.SYS file that determines the maximum number of files that can be open at one time. If the #1 <I>FileNumber</I> was in use by another file that you opened earlier in the application, you could assign the open file to a different number like this:<BR><BR><PRE><FONT COLOR="#000080">Open &quot;d:\data\myfile.dat&quot; For Append As #5</FONT></PRE><P>Any currently unused <I>FileNumber</I> works; you can't associate more than one file at a time to the same <I>FileNumber</I> value.<BR><P>The following Open would open the same file for input in a different program:<BR><BR><PRE><FONT COLOR="#000080">Open &quot;d:\data\myfile.dat&quot; For Input As #2</FONT></PRE><P>Visual Basic supplies a helpful built-in function named FreeFile() that accepts no arguments. FreeFile() returns the next available file number value. For example, if you've used #1 and #2 for open files, the next value returned from FreeFile() will be 3. FreeFile() is most helpful when you write general-purpose subroutine and function procedures that need to open files, and the procedures may be called from more than one place in an application. At each calling location, there is a different number of files open at the time. The procedure can store the value of the next available file number like this:<BR><BR><PRE><FONT COLOR="#000080">fNum = FreeFile()</FONT></PRE><P>and use the variable fNum in subsequent Open, I/O, and Close statements. No matter how many files are open, the procedure will always use the next file number in line to open its file.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Open command associates files using file numbers with which the rest of the program will access the file. The three <I>mode</I> values determine how Visual Basic uses the file. If you want to write to a file, you can't use the Input mode, and if you want to read from a file, you can't use Output or Append.<BR><BR><A NAME="E68E135"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Clean Up with </B><B>Close</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Close statement performs the opposite job from Open. Close closes the file by writing any final data to the file, releasing the file to other applications, and giving the file's number back to your application in case you want to use that number in a subsequent Open statement.<BR><P>Eventually, every program that opens files should close those files. The Close statement closes files. These are the two formats of Close:<BR><BR><PRE><FONT COLOR="#000080">Close [[#]<I>FileNumber</I>] [, ..., [#]<I>FileNumber</I>]</FONT></PRE><P>and<BR><BR><PRE><FONT COLOR="#000080">Close</FONT></PRE><P>The first format closes one or more open files, specifying the files by their open file numbers. The pound sign is optional in front of any of the file numbers. The second form of Close closes all files that are currently open. Close closes any open file no matter what mode you used to open the file.<BR>

⌨️ 快捷键说明

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