introduction to file i/o how to read & write data to a disk file

5
Introduction to File I/O How to read & write data to a disk file...

Upload: olivia-warren

Post on 26-Mar-2015

219 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Introduction to File I/O How to read & write data to a disk file

Introduction to File I/O

How to read & write data to a disk file...

Page 2: Introduction to File I/O How to read & write data to a disk file

Steps for Reading a Text File Steps for Reading a Text File and Filling a List Box...and Filling a List Box...

Open the file in VS so the O/S can do the actual Open the file in VS so the O/S can do the actual reading processreading process

Read each line into a temp variable.Read each line into a temp variable.– Text files must be read sequentiallyText files must be read sequentially– You cannot open a file and tell O/S to go to line 24 and You cannot open a file and tell O/S to go to line 24 and

read the contents.read the contents. Add the line we read to a list boxAdd the line we read to a list box Continue reading each line until we reach the end of Continue reading each line until we reach the end of

the file (EOF)the file (EOF) Close the file.Close the file.

Each file needs an ID called a Handle and it acts as a Each file needs an ID called a Handle and it acts as a buffer when data is read from diskbuffer when data is read from disk

Page 3: Introduction to File I/O How to read & write data to a disk file

Here is the code...Here is the code...

1.1. Let’s create a temp variable to hold our line of data:Let’s create a temp variable to hold our line of data:Dim myData as StringDim myData as String

2.2. Open the file: FileOpen(FileNum, FileName, OpenMode)Open the file: FileOpen(FileNum, FileName, OpenMode)FileOpen(1, “A:\Products.Txt”, OpenMode.Input)FileOpen(1, “A:\Products.Txt”, OpenMode.Input) ‘Input = ‘Input =

Read!Read!

3.3. Read each line inside a loop:Read each line inside a loop:Do Until EOF(1) Do Until EOF(1) ‘make sure file numbers match!‘make sure file numbers match!

myData = LineInput(1)myData = LineInput(1) ‘file number 1‘file number 1lbItems.Items.Add(myData)lbItems.Items.Add(myData)

LoopLoop4.4. Close the fileClose the file5.5. FileClose(1)FileClose(1) ‘file number 1‘file number 1

Page 4: Introduction to File I/O How to read & write data to a disk file

Steps for Writing Data From Steps for Writing Data From the List Box to the file...the List Box to the file...

Open the file in VS so the O/S can do Open the file in VS so the O/S can do the actual writing processthe actual writing process

Read each line of data in the list box Read each line of data in the list box Write the line we read to our disk Write the line we read to our disk Continue reading each item in the List Continue reading each item in the List

Box until we reach the endBox until we reach the end Close the file.Close the file.

Remember to make sure we use the Remember to make sure we use the same file handle in our code!same file handle in our code!

Page 5: Introduction to File I/O How to read & write data to a disk file

Here is the code...Here is the code...

1.1. Let’s create a temp variable to count the items in the list box:Let’s create a temp variable to count the items in the list box:Dim x as IntegerDim x as Integer

2.2. Open the file: FileOpen(FileNum, FileName, OpenMode)Open the file: FileOpen(FileNum, FileName, OpenMode)FileOpen(2, “A:\Products.Txt”, OpenMode.Output)FileOpen(2, “A:\Products.Txt”, OpenMode.Output)

Output mode: creates file if it needs to and writes dataOutput mode: creates file if it needs to and writes dataIf the file exists, it will If the file exists, it will ERASE ALL DATA AND ADD NEW STUFF!!!!ERASE ALL DATA AND ADD NEW STUFF!!!!

Append Mode: creates file if it does not exist and ADDS dataAppend Mode: creates file if it does not exist and ADDS dataIf the file already exists, it adds the new data to the end of the fileIf the file already exists, it adds the new data to the end of the file

3.3. Read each item in the list box:Read each item in the list box:For x = 0 to lbItems.Items.Count - 1For x = 0 to lbItems.Items.Count - 1

PrintLine(2, lbItems.Items(x))PrintLine(2, lbItems.Items(x)) ‘file number 2‘file number 2Next xNext x

4.4. Close the fileClose the file5.5. FileClose(2)FileClose(2) ‘file number 2‘file number 2