[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

How to do 'positioning' on a text file in read mode?

1. we can read a text file 'sequentially' , 1 line (1 record) at a time, with the following VB code:
   option explicit
   dim ch_read as integer    '  file pointer as my reading channel
   dim a_record as string    '  one line of the record for various length as string

   open "text_file.txt" for input as #file_nbr
   Line Input #ch_read, a_record
   ... so you read (Forward) a few line of this string into a_record

2. Questions:  how to 'rewind' the read N records, so I can read forward again?
    I used to see 'rewind N'  statment in some old basic or f77 language a while ago.
    there is a seek() statement in VB, but it only seeking through number of bytes
   from beginning, and I need to keep track it.  alghough this is not totally impossible
   (for example, the length of each record + 2 (LFCR), or somthing like this.)

3. I am looking for the 'rewind N' record statement in VB6
   also, can we do 'BOF'  --- positoin the next reading position at 'begging of the file'
   or  'EOF'   end of file, so we can do 'append' statement.

I hope someone can help me on this simple and useful mechanism to move around a 'file'
with one record at a time.  -- forward, and backward.

Regards,

mshox1

0
mshox1
Asked:
mshox1
1 Solution
 
mvidasCommented:
mshox1,

If you will need to 'move around' within the text file, I find it easiest to read the file into a string array, then navigate within the array easily.  It is quick to load, and much more versatile.  You can use a function like:

Function GetTextFile(ByVal vFile As String) As String()
 Dim FileCont() As String, Cnt As Long, vFF As Long, tempStr As String
 ReDim FileCont(0)
 Open vFile For Input As #vFF
 Do Until EOF(vFF)
  Line Input #vFF, tempStr
  ReDim Preserve FileCont(Cnt)
  FileCont(Cnt) = tempStr
  Cnt = Cnt + 1
 Loop
 Close #vFF
 GetTextFile = FileCont
End Function

To get the file into the array, then do whatever you need to with it.  Please don't hesitate to ask if you have any questions about it!
Matt
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is a another way to get the lines of the file into an array:
Private Sub Command1_Click()
    Dim lines() As String
    lines = Split(GetFileContents("c:\someFile.txt"), vbCrLf)
   
    Dim i As Integer
    For i = 0 To UBound(lines)
        Debug.Print lines(i)
    Next i
End Sub

Private Function GetFileContents(ByVal fileName As String) As String
    Dim ff As Integer
    Dim entireFile As String
   
    If Dir(fileName) <> "" Then
        ff = FreeFile
        Open fileName For Binary Access Read As #ff
        GetFileContents = Input(LOF(ff), ff)
        Close #ff
    Else
        MsgBox fileName, vbExclamation, "File Not Found"
    End If
End Function
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Unfortunately you can't use the Seek() method unless you open the file in Binary or Random modes.  In those modes you can only use the Get and Put methods to access the file and can no longer read it line by line with Line Input.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you make your file fixed width, that is each line is a prefedined length, and lines cannot be longer than that (and shorter lines will be padded with spaces to fit) then you can open the file in Random mode and move back and forth between records in either direction or even jump to a specific record in the file.

Here are some PAQs dealing with Random Access Files:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21233736.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20884901.html
0
 
clarkscottCommented:
Here's another way.....

Open the text file as a random access (fixed length) with a record size of 1.   Loop through and GET each character.  Concatenate he string until you reach the line delimiter (carriage return or line feed).

You can track the starting byte of each line - then simply jump back to the line of your choice and review.

Create a module and place this code.

Type x
s As String * 1
End Type
Public FS As x


Create a button on a form and place this code:


Private Sub Command0_Click()

'------ 100000 = number of possible TEXT LINES in text file -----
Dim vStartLine(1 To 100000) As Long
Dim vRecCount As Long
Dim vString As String

Dim f As Long
f = FreeFile
Dim vFTot As Long

Open "c:\YourTextFile.txt" For Random As #f Len = Len(FS)
vFTot = LOF(f) / Len(FS)

Dim j As Long

vRecCount = 1
vStartLine(vRecCount) = 1

For j = 1 To vFTot
    Get #f, j, FS
    '---------- look for line delimiter (carriage return = chr(13) - Line Feed = chr(10) --------
    If FS.s <> Chr(13) And FS.s <> Chr(10) Then
        '---- concatenate string --------
        vString = vString & FS.s
    Else
        '-- only count logical recs if string is greater than 1 character (could be a 'blank' line)----
        If Len(vString) > 1 Then
            '---- do something with the string ---

            '--- depending on logic - reset j to equal vStartLine(WhateverRecValueYouWish) and go back through--
            '---- for instance.....
            '---- if Left(vstring,1) = "@" then J = vStartLine(vRecValue-1) --- go back 1 logical line ----
           
            '--- these are examples of vStartLine() -----
            '--- vStartLine(1) = 1
            '--- vStartLine(2) = 99
            '--- vStartLine(3) = 200


            '---- clear the string for next logical line ---
            '--- don't add recs if reviewing data (if 'rewinding') ----
            If j > vStartLine(vRecCount) Then
                vRecCount = vRecCount + 1
                vStartLine(vRecCount) = j
            End If
        End If
        vString = ""
    End If


Next j

Close



Scott C.


End Sub


0
 
clarkscottCommented:
After thinking about this for a moment.... this line should probably be

            '---- clear the string for next logical line ---
            '--- don't add recs if reviewing data (if 'rewinding') ----
            If j > vStartLine(vRecCount) Then
                vRecCount = vRecCount + 1

               '--- since the character of J is the line delimiter - vStartLine(vReccount) should be J + 1 ( which would be the
               '--- next byte ----
                vStartLine(vRecCount) = j + 1

            End If


Scott C.
0
 
mshox1Author Commented:
Thank you everyone for your great input.

I think my really question is if VB have the 'rewind nbr_of_records' capability,
(for variable length record)

Obviously there is non.  
I do know how to deal this with loading the file into dynamic array, or treat as random access
(fixed length) ... and understand different 'side affect' have and need to be address.  I thank you
everyone's input.

I would like to 'close' this discussion chapter on this subject.

thanks

mshox1
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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