English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I created a sequential file and i want to acccess record by record i.e. one record at a time.
Is it possible?
If it is ,can i know how to do it?

2007-02-28 23:40:41 · 4 answers · asked by Ninja 2 in Computers & Internet Programming & Design

4 answers

Sub ReadEachRecord()
Dim theString As String = ""
Dim objReader As New TextReader("C:\path\to\file.txt")
Do
theString = objReader.ReadLine()
Console.WriteLine(theString)
Loop Until theString Is Nothing
objReader.Close()
End Sub

2007-03-01 00:33:31 · answer #1 · answered by Anonymous · 0 0

In order to access records stored in a text format you need to know the layout of how each record is stored.

Your records can be of fixed lenght, meaning the smae number of bytes are used for each record nomatter how many data fields are entered. OR Variable lenght records where whitespace (non printing characters like spaces) is not saaved and each record can have a different number of bytes.

The most common is to use variable lenght where each line in the text file represents a complete record. The Data fields of each record are seperated by a delimiter character like a TAB or a COMMA (hence CSV for comma seperated value)

You can also have fixed lenght data fields allocating specific positions on a line to specific data. Fro example positions 0to15 contain the last name while positions 16 to 30 contain the first name.

Anyway the best way to access text data is by using a Stream, or TextReader object. Yes this is similar to teh old fashioned BASIC LINE and FILE# commands but is faster and gives you different options to read data from the file.

Dim ts As IO.StreamReader
Dim str As String
Dim asciiCode As Int32
Dim buffer As Char()

ReDim buffer(15) 'initialize buffer
ts = IO.File.OpenText("C:\myDir.txt")
Do While ts.Peek >= 0 'test for end of data
str = ts.ReadLine 'reads an entire line
ts.ReadBlock(buffer, 0, 10) 'reads the next 10 characters

Debug.WriteLine(str) 'displays the line just read

Debug.WriteLine(buffer(0))
Debug.WriteLine(buffer(1))
Debug.WriteLine(buffer(2))
Debug.WriteLine(buffer(3))
Debug.WriteLine(buffer(4))
Debug.WriteLine(buffer(5))
Debug.WriteLine(buffer(6))

asciiCode = ts.Read 'read a single character
Debug.WriteLine(Chr(asciiCode))
Loop

ts.ReadLine reads one line at a time from the file
ts.ReadBlock reads a range of characters into an array
ts.Read read a single character at a time as its ASCII character code

Between any and all of these methods you can read your sequential file. The method you choose will depend on how your data is stored.

My preferred method is to create CSV data with one record per line. Read a record(line) at a time and split the record into a string array called field() using the comma as the delimiter.

field() = split(str,",")

2007-03-01 00:54:53 · answer #2 · answered by MarkG 7 · 1 0

It is recommended that you use File System Objects to create text files, but this information is provided in case you need to use the older text file creation methods.

Sequential access works best when you want to process files consisting only of text, such as the files created with a typical text editor — that is, files in which data is not divided into a series of records. Sequential access may not be well suited for storing long series of numbers, because each number is stored as a character string. A four-digit number would require 4 bytes of storage instead of the 2 bytes it requires to store the same number as an integer.

2007-03-01 00:11:56 · answer #3 · answered by daveajie 2 · 0 1

Sequential file is not based on records, you can instead read it line by line using
Line Input #1, se1

after you should save the file treating each line as a record.
I always do this

Enjoy my profile, I am the VBAXLMan

2007-02-28 23:47:53 · answer #4 · answered by Anonymous · 0 1

fedest.com, questions and answers