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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing Text File Output

Status
Not open for further replies.

Laurelin

Programmer
Jan 24, 2002
29
0
0
US
I have been tasked with taking a text file and creating a new text file output that has slightly different data based on what is in the original file.

I fully understand how to read a text file and write that data to a new text file. What I cannot fully understand is how to read a field in the original text file and create new data to put in the output file.

I have to look at a field called COUNTY and then determine if the value in the COUNTY field belongs in one of 4 different regions. Once I determine which region it belongs in I need to place that value in the new output file.

There are about 90 different counties and they are broken up into 4 regions(F1, F2, F3, F4). How would I go about grouping all of the counties into the proper region and then putting that into the new file as I read each record of the input file?

Any help would be appreciated. And if you need more information or a better explanation just ask!


 
My first thought is that you shouldn't process the file line by line (read one line, then process the output).
I've been working back in VB6 so forgive me if the terms are not correct. If I understand correctly, I would use an array of some sort to store the read values, then loop through the array:
for i = 0 to ubound(array)
if value is associated with F1 Then
process output
end if
next i

for i = 0 to ubound(array)
if value is associated with F2 Then
process output
end if
next i

...

This is just one (simple and quick) example of how to handle it. There are many different options available.

If at first you don't succeed, then sky diving wasn't meant for you!
 

Assuming that the Region field will be added to each line (record), you would do it as follows. This also assumes that the text file is already open for reading.

Dim County As String
Dim ThisLine as string
Dim StartLocation As Integer
Dim Length As Integer

Do While sr.Peek <> -1 'sr is the streamreader for the text file

ThisLine = sr.ReadLine()
County = ThisLine.Substring(StartLocation, Length)

'note: you need to determine the start location and length
'of each county, for use in the above line of code

ThisLine &= GetRegion(County) 'append Region to end of line

sw.WriteLine(ThisLine) 'sw is a streamwriter for the new text file

Loop



Private Function GetRegion(ByVal County As String) As String

Dim sRetVal as String

'code to get the county's region goes here

sRetVal = RegionCode '(F1, F2, F3 or F4)

Return sRetVal

End Function



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks for the help! I've got it working thanks to both of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top