File Input Output

I am looking for samples of all the different file input output modes
visualbasicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

deightonprogCommented:
This example illustrates various uses of the Open statement to enable input and output to a file.

The following code opens the file TESTFILE in sequential-input mode.

Open "TESTFILE" For Input As #1
' Close before reopening in another mode.
Close #1


This example opens the file in Binary mode for writing operations only.

Open "TESTFILE" For Binary Access Write As #1
' Close before reopening in another mode.
Close #1


The following example opens the file in Random mode.  The file contains records of the user-defined type Record.

Type Record      ' Define user-defined type.
      ID As Integer
      Name As String * 20
End Type

Dim MyRecord As Record      ' Declare variable.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
' Close before reopening in another mode.
Close #1


This code example opens the file for sequential output; any process can read or write to file.

Open "TESTFILE" For Output Shared As #1
' Close before reopening in another mode.
Close #1


This code example opens the file in Binary mode for reading; other processes cannot read file.

Open "TESTFILE" For Binary Access Read Lock Read As #1
0
deightonprogCommented:
This example uses the Input # statement to read data from a file into two variables.  This example assumes that TESTFILE is a file with a few lines of data written to it using the Write # statement; that is, each line contains a string in quotations and a number separated by a comma, for example, ("Hello", 234).

Open "TESTFILE" For Input As #1      ' Open file for input.
Do While Not EOF(1)      ' Loop until end of file.
      Input #1, MyString, MyNumber      ' Read data into two variables.
      Debug.Print MyString, MyNumber      ' Print data to Debug window.
Loop
Close #1      ' Close file.

This example uses the Line Input # statement to read a line from a sequential file and assign it to a variable.  This example assumes that TESTFILE is a text file with a few lines of sample data.

Open "TESTFILE" For Input As #1      ' Open file.
Do While Not EOF(1)      ' Loop until end of file.
      Line Input #1, TextLine      ' Read line into variable.
      Debug.Print TextLine      ' Print to Debug window.
Loop
Close #1      ' Close file.


This example uses the Write # statement to write raw data to a sequential file.

Open "TESTFILE" For Output As #1      ' Open file for output.
Write #1, "Hello World", 234      ' Write comma delimited data .
Write #1,      ' Write blank line.

' Assign Boolean, Date, Null, and Error values.
MyBool = False : MyDate = #February 12, 1969# : MyNull = Null
MyError = CVErr(32767)
' Boolean data is written as #TRUE# or #FALSE#. Date literals are
' written in universal date format, for example, #1994-07-13#
 'represents July 13, 1994. Null data is written as #NULL#.
' Error data is written as #ERROR errorcode#.
Write #1, MyBool ; " is a Boolean value"
Write #1, MyDate ; " is a date"
Write #1, MyNull ; " is a null value"

Write #1, MyError ; " is an error value"

Close #1      ' Close file.

This example uses the Print # statement to write data to a file.

Open "TESTFILE" For Output As #1      ' Open file for output.
Print #1, "This is a test"      ' Print text to file.
Print #1,      ' Print blank line to file.
Print #1, "Zone 1"; Tab ; "Zone 2"      ' Print in two print zones.
Print #1, "Hello" ; " " ; "World"      ' Separate strings with space.
Print #1, Spc(5) ; "5 leading spaces "      ' Print five leading spaces.
Print #1, Tab(10) ; "Hello"      ' Print word at column 10.

' Assign Boolean, Date, Null and Error values.

MyBool = False : MyDate = #February 12, 1969# : MyNull = Null
MyError = CVErr(32767)
' True, False, Null, and Error are translated using locale settings of
' your system. Date literals are written using standard short date
' format.
Print #1, MyBool ; " is a Boolean value"
Print #1, MyDate ; " is a date"
Print #1, MyNull ; " is a null value"
Print #1, MyError ; " is an error value"
Close #1      ' Close file.


This example uses the Get statement to read data from a file into a variable.  This example assumes that TESTFILE is a file containing five records of the user-defined type Record.

Type Record      ' Define user-defined type.
      ID As Integer
      Name As String * 20
End Type

Dim MyRecord As Record      ' Declare variable.
' Open sample file for random access.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
' Read the sample file using the Get statement.
Position = 3      ' Define record number.
Get #1, Position, MyRecord      ' Read third record.
Close #1      ' Close file.


This example uses the Put statement to write data to a file.  Five records of the user-defined type Record are written to the file.

Type Record      ' Define user-defined type.
      ID As Integer
      Name As String * 20
End Type

Dim MyRecord As Record      ' Declare variable.
' Open file for random access.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
For RecordNumber = 1 To 5      ' Loop 5 times.
      MyRecord.ID = RecordNumber      ' Define ID.
      MyRecord.Name = "My Name" & RecordNumber      ' Create a string.
      Put #1, RecordNumber, MyRecord      ' Write record to file.
Next RecordNumber
Close #1      ' Close file.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JuiletteCommented:
Wow...you gotta like it!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.