gongsher
asked on
Internal Sort Program
This program is required to read a text file of country currency rates into array, sort the array, then output the sorted array as a text file. How do you load the text file into array and how do you sort it?
ASKER
Its a sequential file that has a file size of maximum 50 records, record length of fixed 40 bytes and the record layout should be the following:
CountryCode text 4 bytes
CountryName text 20 bytes
BuyRate text 8 bytes
SellRate text 8 bytes
Example of input file:
05 Australia 0.8656 0.8498
02 Japan 62.54 61.08
Up to about 50 records
Have to load the input file records into the array on User selection of the Sort button. It should sort the array elements into ascending Country Code order and then output into another text file.
CountryCode text 4 bytes
CountryName text 20 bytes
BuyRate text 8 bytes
SellRate text 8 bytes
Example of input file:
05 Australia 0.8656 0.8498
02 Japan 62.54 61.08
Up to about 50 records
Have to load the input file records into the array on User selection of the Sort button. It should sort the array elements into ascending Country Code order and then output into another text file.
Look at a recent word joust between me and Waty at https://www.experts-exchange.com/topics/comp/lang/visualbasic/Q.10078969!
ASKER
First, I would just like to know how exactly do you load the text file into array?
What I have done is:
' Opening the input file which contains 40 bytes
Open (App.Path & "\Input.txt") For Random As #1 Len = 40
How do you load the file as array and then output it to another text file? The file contains 50 records and the output should be like the following:
05 Australia 0.8656 0.8498
02 Japan 62.54 61.08
Up to 50 records.
Thanks!
What I have done is:
' Opening the input file which contains 40 bytes
Open (App.Path & "\Input.txt") For Random As #1 Len = 40
How do you load the file as array and then output it to another text file? The file contains 50 records and the output should be like the following:
05 Australia 0.8656
02 Japan 62.54
Up to 50 records.
Thanks!
ASKER
As I mentioned above, I would like to know firstly how to load a text file into an array.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
dabellei
I have written a code here but it is definitely not right. Just wondering if you can have a check and correct. The form should only have one button, (sort button) and a message will come up after sorting, displaying the number of records sorted.
It should open the "ERInput.txt" file, load into array, sort the array and then output the sorted file into ascending Country Code order into the file "EROutput.txt"
Option Explicit
Type ExchangeRate
strCountryCode as string *4
strCountryName as string *20
strBuyRate as string *8
strSellRate as string *8
End Type
Public gExchangeRate (1 to 50) as ExchangeRate
Option Explicit
Dim intNumber As Integer
Dim intIndex As Integer
Dim intArrayNum As Integer
Dim intPrevArrayNum As Integer
Dim udtCountryRate As ExchangeRate
Private Sub cmdSort_Click()
Open (App.Path & "\ERInput.txt") For Random As #1 Len =_ Len(gExchangeRate(1))
intNumber = LOF(1) / Len(gExchangeRate(1))
If LOF(1) / Len(gExchangeRate(1)) > 0 Then
For intIndex = 1 To LOF(1) / Len(gExchangeRate(1))
Get #1, intIndex, gExchangeRate(intIndex)
Next intIndex
End If
For intArrayNum = 2 To intNumber
udtCountryRate = gExchangeRate(intArrayNum)
For intPrevArrayNum = intArrayNum - 1 To 1 Step -1
If Val(udtCountryRate.strCoun tryCode) >= Val(gExchangeRate(intPrevA rrayNum). _
strCountryCode) Then
Exit For
Else
gExchangeRate(intPrevArray Num + 1) = gExchangeRate(intPrevArray Num)
End If
Next intPrevArrayNum
gExchangeRate(intPrevArray Num + 1) = udtCountryRate
Next intArrayNum
Open (App.Path & "\EROutput.txt") For Output As #2 Len = Len(gExchangeRate(1))
For intArrayNum = 1 To intNumber
'Put #2, (intArrayNum), gExchangeRate(intArrayNum)
Print #2, gExchangeRate(intArrayNum) .strCountr yCode;
Print #2, gExchangeRate(intArrayNum) .strCountr yName;
Print #2, gExchangeRate(intArrayNum) .strBuyRat e;
Print #2, gExchangeRate(intArrayNum) .strSellRa te;
Next intArrayNum
Close #2
lblMessage = intNumber & " exchange rate records sorted."
Exit Sub
End Sub
I have written a code here but it is definitely not right. Just wondering if you can have a check and correct. The form should only have one button, (sort button) and a message will come up after sorting, displaying the number of records sorted.
It should open the "ERInput.txt" file, load into array, sort the array and then output the sorted file into ascending Country Code order into the file "EROutput.txt"
Option Explicit
Type ExchangeRate
strCountryCode as string *4
strCountryName as string *20
strBuyRate as string *8
strSellRate as string *8
End Type
Public gExchangeRate (1 to 50) as ExchangeRate
Option Explicit
Dim intNumber As Integer
Dim intIndex As Integer
Dim intArrayNum As Integer
Dim intPrevArrayNum As Integer
Dim udtCountryRate As ExchangeRate
Private Sub cmdSort_Click()
Open (App.Path & "\ERInput.txt") For Random As #1 Len =_ Len(gExchangeRate(1))
intNumber = LOF(1) / Len(gExchangeRate(1))
If LOF(1) / Len(gExchangeRate(1)) > 0 Then
For intIndex = 1 To LOF(1) / Len(gExchangeRate(1))
Get #1, intIndex, gExchangeRate(intIndex)
Next intIndex
End If
For intArrayNum = 2 To intNumber
udtCountryRate = gExchangeRate(intArrayNum)
For intPrevArrayNum = intArrayNum - 1 To 1 Step -1
If Val(udtCountryRate.strCoun
strCountryCode) Then
Exit For
Else
gExchangeRate(intPrevArray
End If
Next intPrevArrayNum
gExchangeRate(intPrevArray
Next intArrayNum
Open (App.Path & "\EROutput.txt") For Output As #2 Len = Len(gExchangeRate(1))
For intArrayNum = 1 To intNumber
'Put #2, (intArrayNum), gExchangeRate(intArrayNum)
Print #2, gExchangeRate(intArrayNum)
Print #2, gExchangeRate(intArrayNum)
Print #2, gExchangeRate(intArrayNum)
Print #2, gExchangeRate(intArrayNum)
Next intArrayNum
Close #2
lblMessage = intNumber & " exchange rate records sorted."
Exit Sub
End Sub
i'll take a look at your code and return with it soon
3 questions. I will add 1 more:
How does your file look like. Is it coma separated?