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

📄 vel18.htm

📁 简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的
💻 HTM
📖 第 1 页 / 共 3 页
字号:
hold data such as customer balances and employee pay values, however, the number of records varies because you'll add and remove records as transactions take place.<BR><P>The Eof() function is Visual Basic's built-in <I>end of file</I> function that senses when an input reaches the end of file. Here is the format of the Eof() function:<BR><BR><PRE><FONT COLOR="#000080">Eof(FileNumber)</FONT></PRE><P>Eof() returns True if the most recent reading of the input file just reached the end of the file and returns False if the input file still has data left to be read. Most data input programs loop until the Eof() condition is true. Perhaps the best way to use Eof() is with a Do Until-Loop that follows this general format:<BR><PRE><FONT COLOR="#000080">Input #1, VariableList ' Read the first recordDo Until (Eof(FileNumer) = True) ' Process the record just read Input #1, VariableList ' Get more dataLoop</FONT></PRE><P>If there are none, one, or four hundred records in the file, this format of Do Until will keep reading, but will stop as soon as the end of file is reached. Many programmers often increment an integer counter variable inside the loop to count the number of records read. The counter is useful if you're reading the file's data into arrays.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>If you read file data into arrays, be sure to dimension more than enough array elements to hold the maximum number of records expected.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 18.5 reads the file written earlier using a series of Write# statements back in Listing 18.3. The body of the code isn't shown. The code is supposed to output the file's contents to a printed paper report. You won't master the reporting commands until Lesson 11, so comments were used in place of the actual reporting commands.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>As long as you output to files using Write#, you'll easily read that same data back again using Input#. Input# is the mirror-image command to Write#. The combination of Write# and Input# makes file I/O simpler than possible in most programming languages.<BR><P><FONT COLOR="#000080"><B>Listing 18.5. Reading and reporting file data using </B><B>Input#</B><B>.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub ReadData ()2: ' Reads array data from a file and reports the data3: ' Assume that 200 values were read4: Static CNames(200) As String, CBalc(200) As Currency5: Static CDate(200) As Variant, CRegion(200) As Integer6: Dim NumVals As Integer ' Count of records7: Dim ctr As Integer ' For loop control8: 9: NumVals = 1 ' Start the count10: ' Reads the file records assuming11: ' four values on each line12: Open &quot;c:\mktg.dat&quot; For Input As #113: Input #1, CNames(NumVals), CBalc(NumVals), CDate(NumVals), CRegion(NumVals)14: Do Until (Eof(1) = True)15: NumVals = NumVals + 1 ' Increment counter16: Input #1, CNames(NumVals), CBalc(NumVals), CDate(NumVals), CRegion(NumVals)17: Loop18:19: ' When loop ends, NumVals holds one too many20: NumVals = NumVals - 121: 22: ' The following loop is for reporting the data23: For ctr = 1 To NumVals24: ' Code goes here that outputs the array25: ' data to the printer26: '27: Next ctr28: Close #129:30: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Lines 4 and 5 define four arrays that will hold the file's data. The arrays are defined to hold, at most, 200 values. There is no error checking to be sure that the reading doesn't read more than 200 records, but an exercise at the end of the chapter gives you the chance to add this array-checking code.<BR><P>Line 9 initializes NumVals to hold the count of file records. The count begins at 1 because the first array subscript that will hold incoming data is 1. (As with all programs in this book, this code ignores the 0 subscript that all arrays contain unless you specify Option Base 1.)<BR><P>Line 12 opens the file for input and associates the file to the file number 1. The first input occurs on line 13. The input attempts to read the first record in the file that contains four values. Line 14's relational test for an end of file condition will immediately be true if the file is empty. Otherwise, the body of the loop executes. Line 15 increments the record count once more owing to line 16's input into the next set of array elements. The loop continues until the end of file is reached and line 14's relational test becomes false.<BR><P>Line 20 must decrement the NumVals variable by 1 because the last attempt at reading the file will always produce an end-of-file condition. Therefore, the final Input# was unsuccessful and the record count can't contain that unsuccessful read.<BR><P>After you read all the data into the arrays, you can work with the array data just as you normally work with array data. You can calculate statistics based on the data, display the array using list boxes and combo boxes, or print the data to paper.<BR><BR><A NAME="E68E138"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Line Input#</B><B> Really Makes it Easy</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Line Input# command reads data from open data files. Unlike Input#, Line Input# reads each line of data in the file into a string variable. You don't have to specify separate variable names after a Line Input# because Line Input# requires a single string value. Line Input# reads data from any file whose lines end with a carriage return and line feed sequence. (Most files do.)<BR><P>The Line Input# command is simple to use for reading entire records into a single variable. Whereas Input# reads each records values individually, Line Input# reads an entire record&#151;data, commas, quotation marks, and everything else&#151;into a string or variant variable or control.<BR><P>Here is the format of Line Input#:<BR><BR><PRE><FONT COLOR="#000080">Line Input #FileNumber, VariableName</FONT></PRE><P>No matter how many record values appear in the file associated with file number 3, the following Line Input# statement reads an image of the record into the string variable named aRecord:<BR><BR><PRE><FONT COLOR="#000080">Line Input #3, aRecord</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Line Input# command reads records from data files into string or variant variables. The Input# command reads each record's individual data values into a single variable (or control). The Line Input# command reads entire records into string or variant variables. The project at the end of this lesson uses Line Input# to read an entire file using a <I>single</I> string variable.<BR><BR><A NAME="E68E139"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E122"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>What is a file?<BR><BR><LI>What are the purposes of files?<BR><BR><LI>True or false: Your computer system can contain more than one file with the same filename.<BR><BR><LI>What statement prepares a file for use by a program?<BR><BR><LI>What are the three modes possible in the Open statement?<BR><BR><LI>What is the purpose of the file number?<BR><BR><LI>If a file doesn't exist and you open the file for output, what does Visual Basic do?<BR><BR><LI>If a file does exist and you open the file for output, what does Visual Basic do?<BR><BR><LI>If a file doesn't exist and you open the file for appending, what does Visual Basic do?<BR><BR><LI>If a file does exist and you open the file for appending, what does Visual Basic do?<BR><BR><LI>If a file doesn't exist and you open the file for input, what does Visual Basic do?<BR><BR><LI>What is the range for the Open statement's file number?<BR><BR><LI>True or false: When opening a file, you must specify the filename using lowercase letters.<BR><BR><LI>What CONFIG.SYS command limits the number of files that you can have open at one time?<BR><BR><LI>Which function locates the next available file number?<BR><BR><LI>Which statement cleans up a file after you're done with the file?<BR><BR><LI>Why is the Close command recommended?<BR><BR><LI>Which open files does the following statement close?<BR><BR><LI>  Close<BR><BR><LI>Which statement writes data to a file?<BR><BR><LI>What character does Write# use to separate values in the output?<BR><BR><LI>What character does Write# place around date and time values?<BR><BR><LI>What character does Write# place around string values?<BR><BR><LI>Which statement is the mirror-image input statement for Write#?<BR><BR><LI>What is a <I>record</I>?<BR><BR><LI>Which function tests for the end of file?<BR><BR><LI>Which statement reads an entire record into a string or variant variable?<BR><BR></OL><BR><A NAME="E69E123"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>What's the Output?</B></FONT></CENTER></H4><BR><OL><LI>What does the file record look like after the following Write# completes?<BR>Write #1, &quot;Peach&quot;, 34.54, 1, &quot;98&quot;<BR><BR></OL><BR><A NAME="E69E124"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><OL><LI>Rusty the programmer wants to read an entire record into a string variable named MyRecord, but his attempt shown as follows doesn't work. Help Rusty fix the problem.<BR>Input #1, MyRecord<BR><BR></OL><BR><A NAME="E69E125"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>Write a Close command that closes an input file associated with file number 3 and an output file associated with file number 19.<BR><BR><LI>Write the Open command needed to open a file named NAMES.DAT for append mode. Associate the file to file number 7.<BR><BR><LI>Change Listing 18.5 to display a message box and exit the file-reading loop if the input data file contains more than 200 records. (The arrays holding the incoming data can't hold more than 200 values.)<BR><BR></OL><BR><A NAME="E69E126"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>Write a subroutine procedure that writes four records of your four best friends' first names, ages, weights, and IQs (remember, these are your friends, so be gentle). Write a second procedure that reads that data into string variables one record at a time.<BR><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="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></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></BODY></HTML>

⌨️ 快捷键说明

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