New at XoJo - Import TEXT File

Here is what I am hoping to do with my first application
(I normally use Clarion for this type of application, but would really like to implement this in XoJo)
I have an ASCII text file, that contains statistical data (flat file)

Each record in the text file, has a standard header, with identifiers .,. These are the record types …
I want to IMPORT these records using XoJo for later reporting

In Clarion, what I would do is grab a record from the text file… Look at the header of the record, say, first 8 bytes, than format the record based on the header type …
Example , if record type abcd then I would branch to a routine that indexed into the record , like this
Field 01 = InputRecord +10 for a Len of 8
Field 02 = InputRecord +19 for a Len of 8
Field 03 = InputRecord +28 for a Len of 8 and so on …
I would then write field01, 02 and 03 to the database, in the case of XoJo I am looking at SQLite

If record type efgh then I would branch to a routine that indexed into the record , like this
Field 0x = InputRecord +10 for a Len of 10
Field 0y = InputRecord +21 for a Len of 10
Field 0z = InputRecord +32 for a Len of 8 and so on …

In essence, I need to read a record from a text file, play with it, and then output fixed length fields / space delimited (mostly numeric)
Is this something I can do with XoJo? If so, are there any examples that can help me get started?

Hi Rick,
check out the BinaryStream project in the “Files” folder under “Examples”.
I just noticed f needs to be checked for existence in the ReadindivualButton Action Hander like in the ReadAll Button Handler. If not if crashes if no file “Elements” exists. But you will find how to read and write several kinds of data.

The code should look like:

[code]Sub Action()
Dim myBoolean As Boolean
Dim myByte As Integer
Dim myDouble As Double
Dim myLong As Integer
Dim mySingle As Single
Dim myPString As String

Dim bs As BinaryStream
Dim f As FolderItem

//get a folderitem
f = GetFolderItem(“Elements”)

//make sure it exists before we try to read it
If f.Exists Then

 //open the binary file without write privelages
 bs = BinaryStream.Open(f, False)

 //make sure we opened it
 If bs <> Nil Then
   //read the values in the same order in which we wrote them during the
   //action event of the writeElements button
   myBoolean = bs.ReadBoolean
   myByte = bs.ReadByte 
   myDouble = bs.ReadDouble 
   myLong = bs.ReadLong 
   mySingle = bs.ReadSingle 
   myPString = bs.ReadPString

   //close the binaryStream
   bs.Close

   //log results into editField
   If myBoolean Then
     OutputArea.Text = "Boolean: True"
   Else
     OutputArea.Text = "Boolean: False"
   End If

   OutputArea.AppendText(EndOfLine + "Byte: " + Str(myByte))
   OutputArea.AppendText(EndOfLine +"Double: " + Str(myDouble))
   OutputArea.AppendText(EndOfLine + "Long: " + Str(myLong))
   OutputArea.AppendText(EndOfLine + "Single: " + Str(mySingle))
   OutputArea.AppendText(EndOfLine + "PString: " + myPString)
 End If

End if
End Sub[/code]

https://documentation.xojo.com/index.php/TextInputStream.ReadLine

Not sure about examples but:

Dim fh As FolderItem = GetFolderItem("") // Open dialog box, stores file selected into fh
Dim tis As TextInputStream = TextInputStream.Open(fh) // Open as a text stream

While Not tis.EOF
  Dim line As String = tis.ReadLine

  If line.InStr("ABC") = 1 Then // Line starts with ABC
    ProcessAbcRecord(line)
  ElseIf line.InStr("DEF") = 1 Then // Line starts with DEF
    ProcessDefRecord(line)
  Else
    Print "Unknown record type: " + line
  End If
Wend

tis.Close

Oh yes sure, Textinputstream is even better for pure text files.

Seems you got a few suggestions! I wasn’t paying attention, I noticed you said the first 8 bytes determines record type. So, to amend my example:

Dim recordType As String = line.Left(8)
Dim recordData As String = line.Mid(9) // if no length provided, will go to the end

Select Case recordType
Case "ABCD1234"
    ProcessAbcd1234(recordData)

Case "WXYZ9876"
  ProcessWxyz9876(recordData)

Case Else
  Print "Unknown record type: " + recordType
End Select