File I/O Examples

If you don't define a specific file path, the default location that PC-DMIS searches for the file to open is "C:\Users\Public\Documents\Hexagon\PC-DMIS\<version>".

This code sample provides various examples of several methods or functions. It contains these subroutines:

Sub Main()
    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.
        MsgBox TextLine                          ' Print to Debug window.
    Loop 
    Close #1                                     ' Close file.
    Call Test                                    ' Call The test subroutine.
End Sub
 
Sub Test()
    Open "MYFILE" For Input As #1                ' Open file for input.
     Do While Not EOF(1)                          ' Check for end of file.
        Line Input #1, InputData                 ' Read line of data.
        MsgBox InputData
    Loop
    Close #1                                     ' Close file.
    Call FileIO_Example
End Sub
 
Sub FileIO_Example()
    Dim Msg                                      ' Declare variable.
    Call Make3Files                            ' Create data files.
    Msg = "Three test files, TEST1, TEST2, and TEST3, have been created on your disk. "
    Msg = Msg & "Click OK to remove these three test files."
    MsgBox Msg 
    For I = 1 To 3
        Kill "TEST" & I                          ' Remove data files from disk.
    Next I
End Sub
 
Sub Make3Files()
    Dim I, FNum, FName                           ' Declare variables. 
    For I = 1 To 3
        FNum = FreeFile                          ' Determine next file number.
        FName = "TEST" & FNum
        Open FName For Output As FNum            ' Open file.
        Print #I, "This is test #" & I           ' Write string to file.
        Print #I, "Here is another ";  "line";  I
    Next I 
    Close                                        ' Close all files.
End Sub