Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using StreamReader, StreamWriter, with a Structure for reco

Status
Not open for further replies.

AustinOne

Programmer
Mar 22, 2002
59
0
0
US
I'm a VB newbie with a COBOL background, and I see that a VB Structure is a way to simulate a COBOL FD (record layout of fields).

I am working on a program to use a loop to read an input textfile one record at a time, process certain fields in the record, and write out the updated record to an output textfile (a second file) - a simple batch process. These are plain old text-editor type textfiles.

I understand that using StreamReader and StreamWriter is the preferred approach (as opposed to FileGet and FilePut).

Since StreamReader ReadLine and Streamwriter WriteLine simply use a single string variable for the whole record, what I want to do is to "map" that single string (representing the entire record as read by ReadLine) to a Structure, process the fields of the Structure, then "map" the Structure back to a single string for the WriteLine to write out.

(I know that FileGet and FilePut can read and write directly to / from structures, but I prefer to use StrearmReader/ReadLine and StreamWriter/WriteLine. I also know that I can do partial Reads and Writes, but don't want to).

What is the best (simplest, most efficient) way to do this "mapping" back and forth between a single string (representing the whole record) and the structure, while adhering to the spirit of the above approach?

Thanks

 
Well, the .Split function of a string takes a string and a delimiter and returns an array of strings. Here's an example based on what I think you're looking to do:

Code:
    Public Structure MyRecord
        Private mField1 As Integer
        Private mField2 As String
        Private mField3 As DateTime
        Private mDelimieter As String
        Sub New(ByVal RecordString As String, ByVal Delimiter As String)
            Me.mDelimieter = Delimiter
            Dim Values() As String = RecordString.Split(Delimiter)
            If Values.Length <> 3 Then Throw New Exception("There must be three fields")
            Me.mField1 = Integer.Parse(Values(0))
            Me.mField2 = Values(1)
            Me.mField3 = DateTime.Parse(Values(2))
        End Sub
        Friend Sub ProcessStuff()
            'Do Stuff
        End Sub
        Public Overrides Function ToString() As String
            Return Me.mField1.ToString & Me.mDelimieter & Me.mField2.ToString & Me.mDelimieter & Me.mField3.ToString
        End Function
    End Structure

Then call it by:
Code:
Dim mr As New MyRecord(SomeString, SomeDelimiter)
mr.ProcessStuff
MessageBox.Show(mr.ToString)
 
RiverGuy, thanks for the solution. I will study it and no doubt learn several things from it.

That said, sorry I forgot to mention that the record has no delimeters between the fields but they are fixed length. I'm also thinking that I should initially load all the fields up as strings and do any converts from there if need be. I'm thinking something like this:

Structure MyRecStructure ' record length = 47 characters
<VBFixedString(9)> Public AcctNo as String ' really an int
<VBFixedString(30)> Public Name as String
<VBFixedString(8)> Public DateLastActYYYYMMDD as String
End Structure

In reality, there are a lot more fields than just 3, so:

1) How could I walk thru a loop, loading these fields from the input record string?
2) How could I walk thru a loop unloading these fields back to the output record string?
 
1) How could I walk thru a loop, loading these fields from the input record string?

You really wouldn't be able to do this in a loop. Your fields are of different lengths, and you're loading discrete variables as opposed to a collection. So I think you're going to be doing something like the following:

Code:
AcctNo = InputString.SubString(0,9)
Name = InputString.SubString(9, 30)
'etc.

2) How could I walk thru a loop unloading these fields back to the output record string?

Here the easiest would be to just override the ToString() function as I showed above, concatenating all of your field variables together.
 
Of course you are right (what was I thinking?).

I am still studying the Structure approach and its Class-like aspect. Truth be known, I am not yet totally comfortable with OOP.

But your examples are helping me get there! Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top