LAB 7.2 - Simple File I/O

CIS241

Goals:

w Create a data file with a text editor

w Read a specified amount of data from a file

w Read data from a file using a flag as end of file marker

w Read data from a file using EOF

w Read data from a file character by character

w Write data to a file

__1. Begin a new VB project, renaming & saving as previously specified.

__2. Open Notepad and create a text file

w Type in 10 integers, each on a separate line, followed by -1

w Save the file as numbers.txt (be sure to save this file in the same

folder as your VB project)

__3. Write a count controlled loop to process the values in that file

w Get some information from online Help

v Click on Help on the menu bar, choose Index, and type Print.

v Double click on Print # statement

v Click on Edit in the MSDN menu bar, choose Find in this Topic

and type in read. What do you think the VB keyword is for

reading from a file?

v Find information on that VB keyword. When you do click on

Example and study the example shown.

w Place a command button on the form and write code to:

v Open the file for input (use the code below)

Open App.Path & "\numbers.txt" For Input As #1

v Read 10 values from the file, summing them. For reading use

a statement similar to the one below

Input #1, val

v Print the sum of those numbers as the caption of a label

v Close the input file.

__4. Add processing to the loop

w Add code to the above to find the minimum and maximum values

read from the input file.

v Declare two variables, say max and min

v Initialize max to the smallest possible integer value

v Initialize min to the largest possible integer value

v Inside the loop add this line of code for min

&#If val < min Then min = val

v And add a similar line for max

w Use labels to output those values to the screen.

__5. Write a flag controlled loop to process the values in that file

w Place a second command button on the form

w Write code for processing using a While-Wend loop

v Place a read statement immediately before the loop

{This is known as a priming read}

v Copy and paste as much of the code from the For loop as

seems appropriate. Make any necessary adjustments.

v Place a second read statement at the very end of the loop body,

immediately before the Wend.

v Let the test condition of the loop be that the value read is -1.

w Test your program. Does this loop give the same results?

__6. Write an EOF controlled loop to process the values in that file

w Check out EOF in online Help

v Type eof into the Help Index

v Double click on EOF Function

v Look over the entry and study the example

w Place a third command button on the form

w Write code for processing controlled by EOF

v Copy and paste the code from the while loop

v Change the test to read as below:

While Not EOF(1)

w Test your program. Does this loop give the same results as before?

__7. Write a loop to process the file a character at a time

w Place a fourth command button on the form & use the code below

Dim chval As String * 1

Open App.Path & "\numbers.txt" For Input As #1

Open App.Path & "\record.txt" For Output As #2

chval = Input(1, #1)

While Not EOF(1)

Print #2, chval, Asc(chval)

chval = Input(1, #1)

Wend

Print #2, chval, Asc(chval)

Close #1

Close #2

w Notice the difference in the line that reads the input file

v Look up Input function in online Help

v Study the example

v What is the difference between Input and Input # ?

{Notice that you can go from the one entry to the other

by clicking on See Also}

w Use online Help to learn about the ASCII character set

w Use that information to study your output file

w What is the 13-10 combination that appears repeatedly in that

file?