问题
In VB2010 I need to process a text file which has multiple lines as follows:
Student name, 65, 42, 71, 55 Student name 2, 40, 42, 54, 45 etc.
I need to process the file, finding:
- the overall end of year mark and grade each user received
- the student who got the highest mark
- the average overall mark
- the list of students who achieved a particular grade that the user can specify.
Grades: 70% - 100% = D, 60% - 70% = P1, 50% - 60% = P2, 40% - 50% = P3, 0% - 39% = F
I am trying to put a data structure into Public Class
Structure Student
Dim name as String
Dim Paper1 as String
Dim Paper2 as String
Dim Paper3 as String
DIm Paper4 as String.
End Structure
'I am also trying to to do an array
Dim Students() as Student
'Open txt File
Dim FileStreamReader As IO.StreamReader
FileStreamReader = IO.File.OpenText("c:/textfile.txt")
Read the File one line at a time
While not fileStreamReader.EndofStream
Dim linein as String
linein = FileStreamReader.readline()
I have two main questions:
- Am I on the right track and if so,
- Any ideas how I can go about manipulating the information from the txt file? I have been trying for days but I keep getting errors!
回答1:
You should be using a dedicated csv parser to parse lines in the file. Do not use regular expressions. Do not use String.Split(). Do not try to write your own. You can use the Microsoft.VisualBasic.TextFieldParser class.
You should not use a struct with fields that can be changed. Make it a class instead of a Structure, or make it all the fields read only (no public Set option) and only change the fields in the constructor.
来源:https://stackoverflow.com/questions/15621756/importing-txt-file-and-manipulating-string-to-do-calculations