VB read and take text???

2010-03-23 5:30 am
I have an problem about read and take the text from one text file, than create the new txtfile to paste on and that should follow the format at below:

1. Each string must be surrounded by double quotes.
Each piece of data must be delimited by a Tab
Each person's data must be stored an a new line

2. The data to be stored is:
Person ID
Person Name
Total Score (addition of all three scores for the person)

3. Once all data has been added to the file, add the following statistics
Average total score, Highest scorer (there may be multiple persons) and Lowest scorer (there may be multiple persons)

the example of the txtfile that used to take txt name: takefile.txt:

Linda Kowalski,735146X,8,15.5,16
Andre Ursini,7924402,12,7,12.5
Julia Jenkins,7931829,8.5,8.5,15

the example of the txtfile that we need to create and paste on that follow the format name: finalfile.txt:
"735146X" "Linda Kowalski" "39.5"
"7924402" "Andre Ursini" "31.5"
"7931829" "Julia Jenkins" "32.0"
==========================================
Average total score: 34.3
Highest scorer : Linda Kowalski
Lowest scorer: Andre Ursini

回答 (1)

2010-03-23 8:10 am
✔ 最佳答案
There are different ways to do it. You can read the entire file content and read the file content line by line. The codes below show how to do it line by line.

Imports System
Imports System.IO
Imports System.Text

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sr As StreamReader = New StreamReader("c:\temp\names.txt")
Dim sw As StreamWriter = New StreamWriter("c:\temp\names2.txt")
Dim high As Double = 0
Dim highscorer As String = ""
Dim low As Double = 999999
Dim lowscorer As String = ""
Dim grandtotal As Double = 0
Dim rowcount As Long = 0

Do Until sr.EndOfStream
' read one line at a time
Dim linedata As String = sr.ReadLine()
rowcount += 1 ' this is the row counter
Dim s() As String = {","} ' comma as separator
' split each line into an array separater by comma
Dim data() As String = linedata.Split(s, StringSplitOptions.RemoveEmptyEntries)
Dim total As Double = Convert.ToDouble(data(2)) + Convert.ToDouble(data(3)) + Convert.ToDouble(data(4))
If total > high Then ' find the high scorer
high = total
highscorer = data(0)
End If
If total < low Then ' find the low scorer
low = total
lowscorer = data(0)
End If
grandtotal += total ' sum all total score
Dim output() As String = {"""", data(1), """", Chr(9), """", data(0), """", Chr(9), """", data(2), """", Chr(9), """", data(3), """", Chr(9), """", data(4), """"}
sw.WriteLine(String.Concat(output))
Loop
Dim average = Decimal.Round(Convert.ToDecimal(grandtotal / rowcount), 1)
sw.WriteLine(average.ToString())
sw.WriteLine(highscorer)
sw.WriteLine(lowscorer)
sw.Flush()
sw.Close()
sr.Close()
End Sub
End Class

2010-03-23 00:15:29 補充:
Change
Dim data() As String = linedata.Split(s, StringSplitOptions.RemoveEmptyEntries)
to
Dim data() As String = linedata.Split(s, StringSplitOptions.None)


收錄日期: 2021-04-25 20:33:53
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20100322000051KK01475

檢視 Wayback Machine 備份