[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

File Input Output

I am looking for samples of all the different file input output modes
0
visualbasic
Asked:
visualbasic
  • 2
1 Solution
 
deightonCommented:
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
 
deightonCommented:
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
 
JuiletteCommented:
Wow...you gotta like it!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now