Tech Note 03m: Accessing Simple Files

January 26, 2006

 
© NSB Corporation. All rights reserved.

There is additional documentation on Microsoft's website..

Simple File I/O can be done in NS BASIC/CE using the File Object. To create a File Object, use the following statement:

addObject "file"

A sample follows showing how Simple File I/O works.

 

File Properties

Properties

Description

Attr

Return file mode used on Open statement.

Eof

Returns True/False if end of a file has been reached.

Loc

Returns the current read/write position within an open file.

Lof

Returns the size (in bytes) of an open file.

Seek

Sets or returns the next read/write position within an open file.

 

File Methods

Type

Method

Description

Open

Open pathname,mode [,access][,lock][,reclen]

Enables input/output (I/O) to a file.

Mode: Specifies the file mode
0: closed
1: input
2: output
4: random
8: append
32: binary

Access: Operations permitted on the file
1: read
2: write
3: read and write (default)

Lock: Operations permitted on the file by other processes
1: shared
2: lock read
3: lock write (default)
0: lock read and write

Reclen can be up to 32767.

Close

Close

Concludes input/output (I/O) to a file opened using the Open statement.

Read

Get data[,recNumber]

Reads data from an open disk file into a variable.

 

InputFields(n)

Reads n datafields from an open sequential file and assigns the data to variables. See WriteFields.

 

Input (number)

Returns a string that contains number characters from a file opened in Input or Binary mode. See Put.

 

InputB (number)

Returns a string that contains number bytes from a file opened in Input or Binary mode..

 

LineInputString()

Reads a single line from an open sequential file and assigns it to a string variable. See linePrint.

Write

LinePrint()

Writes a text string to a file.

 

Put data[,recNumber]

Writes data from a variable to a disk file.

 

WriteFields

Writes data to a sequential file. Puts commas between items and quotes around strings, and a newline at the end of each writeFields list.

 

Sample Code 

 

REM This program demonstrates the use of the File and Filesystem Controls

Option Explicit
Dim selection
addObject "filesystem"
addObject "file"

addObject "commandButton","cmdAttr",5,5,70,20
cmdAttr.text="Attr"
addObject "commandButton","cmdDir",5,25,70,20
cmdDir.text="Dir"
addObject "commandButton","cmdGetPut",5,45,70,20
cmdGetPut.text="Get/Put"
addObject "commandButton","cmdLineWrite",5,65,70,20
cmdLineWrite.text="Line Write"
addObject "commandButton","cmdWrite",5,85,70,20
cmdWrite.text="Write"

addObject "label","label",10,110,230,20
label.text="File Path"
label.backcolor=output.backcolor
addObject "textbox","input",10,130,230,20


Sub cmdAttr_Click()
  On Error Resume Next
  Dim sTheFile
   
  sTheFile = input.text
  
  msgbox "GetAttr:" & fileSystem.GetAttr(sTheFile) & chr(13) & "FileDateTime:" & fileSystem.FileDateTime(sTheFile) & chr(13) & "FileLen:" & fileSystem.fileLen(sTheFile),0,"File Attributes for " & sTheFile
  If Err <> 0 Then
    MsgBox "Error in GetAttr: " & Err.Description
    Err.Clear
  End If
  killfocus
End Sub

Sub cmdDir_Click()
  Dim sDir, sMsg

  label.text="Enter file name (wildcards OK) for DIR Command"

  sDir = filesystem.dir(input.text)
  sMsg = "Directory contents of " & input.text & ": " & vbCrLf & sDir
  While (sDir <> "")
    sDir = fileSystem.Dir()
    If sDir <> "" Then
      sMsg = sMsg & ", " & sDir
    End If
  Wend
   
  msgbox  sMsg
  killfocus
End Sub

Sub cmdGetPut_Click()
  WriteUsingPut
  ReadUsingGet
  killfocus
End Sub

Sub cmdLineWrite_Click()
  Writeline
  Readline
  killfocus
End Sub

Sub cmdWrite_Click()
  WriteUsingControl
  ReadUsingControl
  killfocus
End Sub

Sub WriteUsingPut()
  On Error Resume Next
  Dim myArray
  ReDim myArray(6)
  file.Open "ceGetPut.txt", 4, 2, 3, 500 'fsAccessWrite

  myArray(1) = 1
  myArray(2) = "This is a string"
  myArray(3) = True
  myArray(4) = Date
  myArray(5) = 63.12

  file.Put myArray
  If Err <> 0 Then
    MsgBox "Error in WriteUsingPut: " & Err.Description
    err.clear
  End If

  myArray(1) = 2
  myArray(2) = "This is a string2"
  myArray(3) = True
  myArray(4) = Date + 1
  myArray(5) = 63.12
  file.Put myArray

  myArray(1) = 3
  myArray(2) = "This is a string3"
  myArray(3) = True
  myArray(4) = #11/16/34#
  myArray(5) = 63.12
  file.Put myArray

  msgbox "done with Put", 0, "Put"
  file.Close
End Sub

Sub ReadUsingGet()
  Dim myArray,i,msgString
  On Error Resume Next
  file.Open "ceGetPut.txt", 4, 1, 3, 500
  file.Get myArray, 2

  msgString = myArray(1)
  For i = 2 To UBound(myArray)
    msgString = msgString & ", " & myArray(i)
  Next

  msgBox msgString,0,"Contents of ceGetPut.txt"
  file.Close
End Sub

Sub WriteUsingControl()
  Dim myArray
  ReDim myArray(10)

  myArray(1) = Date
  myArray(2) = "This is a string with a "" (quote)."
  myArray(3) = "True"
  myArray(4) = #11/16/34#
  myArray(5) = 63.12
  myArray(6) = "Text"
  myArray(7) = "Null"
  myArray(8) = ""

  file.Open "ceWrite.txt", 2 'fsModeOutput
  file.WriteFields myArray
  file.Close
End Sub

Sub ReadUsingControl()
  Dim myArray, msgString, i
  file.Open "ceWrite.txt", 1 'fsModeInput
  myArray = file.InputFields(10)
    
  msgString = myArray(1)
  For i = 2 To UBound(myArray, 1)
    msgString = msgString & ", " & myArray(i)
  Next
    
  msgbox msgString,0,"Contents of ceWrite.txt"
  file.Close
End Sub

Sub Writeline()
  On Error Resume Next
  file.Open "ceLine.txt", 2 'fsModeOutput
  If Err <> 0 Then
    MsgBox "Error in writeline: " & Err.Description
    Err.Clear
  End If
  file.LinePrint "Hier ist unser erste Linie."
  file.LinePrint "Hier ist unser zweite Linie."
  file.Close
End Sub

Sub Readline()
  file.Open "ceLine.txt", 1 'fsModeInput
  dim sOut
  sOut = file.LineInputString()
  sOut =sOut & Chr(13) & file.LineInputString()
  msgBox sOut,0,"Contents of ceLine.txt"

  file.Close
End Sub