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

📄 vel18.htm

📁 简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>If you create a file by opening the file with the Output <I>mode</I>, and then close the file, you can reopen the same file in the same program in the Input <I>mode</I> to read the file.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The following statement closes the two open files that were opened and attached to the 1 and 3 file numbers:<BR><BR><PRE><FONT COLOR="#000080">Close 1, 3</FONT></PRE><P>The following statement closes <I>all</I> files no matter how many are open:<BR><BR><PRE><FONT COLOR="#000080">Close ' Closes ALL files</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Use the Close statement to close all open files before your program ends. Closing files provides extra safety, so close any and all open files when you're through accessing those files. If a power failure occurs during your program's execution, your closed files will be safely stored on the disk, but any files that are open at the time of the power failure could lose data.<BR><BR><A NAME="E68E136"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Writing to Files With </B><B>Write</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Write# command is perhaps the easier command to use for writing data to a file. Write# writes data of any data type to a file. Using corresponding input statements that you'll learn in the next section, you'll be able to read data that you sent to a file with the Write# command.<BR><P>The Write# statement enables you to write data of any format to any disk file opened in the Output or Append mode. Write# writes strings, numbers, constants, variables, in any and all combinations to a disk file.<BR><P>Here is the format of Write#:<BR><BR><PRE><FONT COLOR="#000080">Write #FileNumber [, ExpressionList]</FONT></PRE><P>The <I>FileNumber</I> must be a file number associated to a file opened with Output. If you don't specify variables or values to write, Write# writes a carriage return and line feed character (an ASCII 13 followed by an ASCII 10) to the file, putting a blank line in the file. If you specify more than one value in the <I>ExpressionList</I>, Visual Basic writes that data to the file using the following considerations:<BR><UL><LI>Write# separates multiple items on the same line by adding commas between values<BR><BR><LI>Write# always adds a carriage return and line feed character to the end of each line written.<BR><BR><LI>Write# adds quotation marks around all strings in the file. The quotation marks make for easy reading of the strings later.<BR><BR><LI>Write# write date and time values using the following format:<BR>#yyyy-mm-dd hh:mm:ss#<BR><BR><LI>Write# writes the value of #NULL# to the file if the data is null (a VarType of 1).<BR><BR><LI>Write# writes nothing when the data value is empty (a VarType of 0), but does separate even empty values with commas if you write more than one value on a single line.<BR><BR></UL><P>The code in Listing 18.1 writes a famous first century poem to a disk file named ODE.TXT. A command button named cmdWrite triggers the code's execution with its Click event when the user clicks the command button. The file appears on your C: drive's root directory.<BR><P><FONT COLOR="#000080"><B>Listing 18.1. Writing four string values to a file named ODE.TXT.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub cmdWrite_Click ()2: ' Creates a text file and3: ' writes a poem to the file4: Open &quot;c:\ode.txt&quot; For Output As #15: 6: ' Write the poem7: Write #1, &quot;Visual Basic, Visual Basic,&quot;8: Write #1, &quot;oh how I long to see...&quot;9: Write #1, &quot;A working application that&quot;10: Write #1, &quot;means so much to me.&quot;11: 12: ' Always close any open file13: ' when done with the file14: Close #115: End Sub</FONT></PRE><P>When Visual Basic closes the file in line 14, there will be a file in the user's root directory with the following contents:<BR><PRE><FONT COLOR="#000080">&quot;Visual Basic, Visual Basic,&quot;&quot;oh how I long to see...&quot;&quot;A working application that&quot;&quot;means so much to me.&quot;</FONT></PRE><P>Typically, quotation marks never appear when you assign or display string values. The quotation marks are usually for the programmer to indicate the start and end of string values used in a program. The only exception to the appearance of quotation marks is that Write# always adds the quotation marks around all string data, whether the data is variable or constant, that Write# outputs to a file.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: Append</I> means to add to the end of something.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>If you open a file using the Append <I>mode</I>, the Write# statement will add to the end of the file. The program in Listing 18.2 writes a blank line and a second stanza to the poem stored in the ODE.TXT file.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>Remember that if you write to a file that already exists using the Output <I>mode</I> value, Visual Basic will erase the original contents and replace the file with the subsequent writing.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#000080"><B>Listing 18.2. Code that adds to the end of a data file using the </B><B>Write#</B><B> statement.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub cmdAddIt_Click ()2: ' Adds to a text file already created3: ' by writing a second verse to the file4: Open &quot;c:\ode.txt&quot; For Append As #15:6: ' Add to the poem7: Write #1, ' Writes one blank line8: Write #1, &quot;Visual Basic, to you I sing&quot;9: Write #1, &quot;the songs a programmer knows...&quot;10: Write #1, &quot;Listen carefully when I choose Run,&quot;11: Write #1, &quot;or we'll surely come to blows.&quot;12:13: Close #114:15: End Sub</FONT></PRE><P>I'll give you a chance to get a handkerchief before looking at the newly expanded poem. Here is the result of the Append <I>mode</I> value if you were to display the contents of the ODE.TXT file after running this event procedure:<BR><PRE><FONT COLOR="#000080">&quot;Visual Basic, Visual Basic,&quot;&quot;oh how I long to see...&quot;&quot;A working application that&quot;&quot;means so much to me.&quot;&quot;Visual Basic, to you I sing&quot;&quot;the songs a programmer knows...&quot;&quot;Listen carefully when I choose Run,&quot;&quot;or we'll surely come to blows.&quot;</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>You may write data to files from variables as well as from controls on the form. Wherever you've got data that needs to be written, Visual Basic's Write# command will write that data to a disk file that you've opened.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 18.3 contains a subroutine procedure that accepts four arrays of four different data types and writes that array data to a disk file named VALUES.DAT. Notice how a simple For loop can be used to write a large amount of data to a data file. The fifth argument sent to the subroutine is assumed to contain the total number of elements defined for the arrays.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Write# statement provides one of the easiest file output commands inside the Visual Basic language. Write# separates multiple output values with commas and encloses all string data inside quotation marks.<BR><P><FONT COLOR="#000080"><B>Listing 18.3. Code that writes arrays using the </B><B>Write#</B><B> statement.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub WriteData (CNames() As String, CBalc() As Currency, CDate() As Variant, CRegion() As Integer)2: ' Writes array data to a file3: Dim ctr As Integer ' For loop control4: ' Assumes that each array has the5: ' same number of elements defined6: Dim MaxSub As Integer7: MaxSub = UBound(CNames) ' The maximum subscript8: 9: ' Write MaxSub lines to the file10: ' with four values on each line11: Open &quot;c:\mktg.dat&quot; For Output As #112: For ctr = 1 To MaxSub13: Write #1, CNames(ctr), CBalc(ctr), CDate(ctr), CRegion(ctr)14: Next ctr15: Close #116:17: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Output: </I></B></FONT>Here are six sample lines from the MKTG.DAT file that the program in Listing 18.3 might write:<BR><PRE><FONT COLOR="#000080">&quot;Adams, H&quot;, 123.41, #1997-11-18 11:34:21#, 6&quot;Enyart, B&quot;, 602.99, #21:40:01#, 4&quot;Powers, W&quot;, 12.17, #1996-02-09#, 7&quot;O'Rourke, P&quot;, 8.74, #1998-05-24 14:53:10#, 0&quot;Grady, Q&quot;, 154.75, #1997-10-30 17:23:59#, 6&quot;McConnell, I&quot;, 9502.32, #1996-07-12 08:00:03#, 9</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Line 1 defines the subroutine procedure and accepts the four passed arrays. Each array is assumed to have the same number of defined subscripts. Although not all arrays passed to a procedure would necessarily have the same number of subscripts defined, these happen to do so. Line 6 and 7 defines and initializes a value that holds the maximum number of subscripts, so the subsequent For loop can write all of the array values to the file names MKTG.DAT.<BR><P>The For loop in lines 12 to 14 step through the array elements, writing a line of four array values with each iteration. Visual Basic encloses the string array data with quotation marks and encloses the variant date and time data with pound signs.<BR><P>The pound signs around the date and time variant values help Visual Basic when you subsequently read the data values back into variant variables. As you can see, the date may have a missing time or the time may have a missing date. Write# still writes as much of the date and time as is available within that variant value.<BR><P>The Close statement on line 15 closes the file and releases the file number back to the program.<BR><BR><A NAME="E68E137"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Inputting from Files with </B><B>Input#</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Input# statement reads data from files and stores the file data in your program's variables and controls. Input# is the mirror-image statement to the Write# statement. Use Input# to read any data that you send to a file with Write#.<BR><P>The Input# statement reads data into a list of variables or controls. Here is the format of Input#:<BR><BR><PRE><FONT COLOR="#000080">Input #FileNumber [, ExpressionList]</FONT></PRE><P>Again, the bottom line to using Input# is that Input# is the mirror image of the Write# statement that produced the file data. When you write a program that must use data from a data file, locate the program's Write# statement that originally created the data file, and use that same format for the Input# statement.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Be sure to open all files that you'll read from using the Input file mode value, or Visual Basic will display an error message.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Listing 18.4 reads and displays in a scrolling list box the poem stored in the ODE.TXT file that was created in earlier listings.<BR><P><FONT COLOR="#000080"><B>Listing 18.4. Code that reads a poem using the </B><B>Input#</B><B> statement.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub cmdList_Click ()2: ' Reads a poem from a text file and3: ' adds the poem, one line at a time,4: ' to the list box5: Dim ctr As Integer ' For loop control6: Dim PoemLine As String ' Holds each poem line7: Open &quot;c:\ode.txt&quot; For Input As #18: 9: ' Read the poem10: For ctr = 1 To 911: Input #1, PoemLine12: lstPoem.AddItem PoemLine13: Next ctr14: 15: ' Always close any open file16: ' when done with the file17: Close #118:19: End Sub</FONT></PRE><P>Figure 18.1 shows the list box that would appear as a result of Listing 18.4's execution.<BR><P><B> <A HREF="18vel01.gif">Figure 18.1. The contents of a file shown in a list box.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>record</I> is a row in a file.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>When reading data from a file, you can very easily cause an error by attempting to read more data than the file holds. Listing 18.4 assumed that there were only nine records in the poem file to read, and that happened to be the case. For data files that 

⌨️ 快捷键说明

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