2014-11-22 203 views
-1
Structure TownType 
    Dim Name As String 
    Dim County As String 
    Dim Population As Integer 
    Dim Area As Integer 
End Structure 

Sub Main() 
    Dim TownList As TownType 
    Dim FileName As String 
    Dim NumberOfRecords As Integer 
    FileName = "N:\2_7_towns(2).csv" 

    FileOpen(1, FileName, OpenMode.Random, , , Len(TownList)) 
    NumberOfRecords = LOF(1)/Len(TownList) 
    Console.WriteLine(NumberOfRecords) 
    Console.ReadLine() 

该文件中只有12个记录,但是该记录数返回值为24。我该如何解决?从csv文件读取和写入

内容CSV文件的:

Town, County,Pop, Area 
Berwick-upon-tweed, Nothumberland,12870,468 
Bideford, devon,16262,430 
Bognor Regis, West Sussex,62141,1635 
Bridlington, East Yorkshire,33589,791 
Bridport, Dorset,12977,425 
Cleethorpes, Lincolnshire,31853,558 
Colwyn bay, Conway,30269,953 
Dover, Kent,34087,861 
Falmouth, Cornwall,21635,543 
Great Yarmouth, Norfolk,58032,1467 
Hastings, East Sussex,85828,1998 
+1

什么是你想在这里做什么?我想你正在尝试创建一个TownType对象集合,但这不是你正在做的事情。 LOF函数以字节为单位返回文件的长度,并将该对象的长度与文件中的记录数量无关。 – Kevin 2014-11-22 12:51:26

+0

最后,我想在控制台上以表格格式显示csv文件的内容,并且能够编辑它们,添加新记录并删除记录,但首先我希望程序计算文件中记录的数量通过将文件中的字节总数除以一条记录中的字节数,但不能给出正确的答案。 – Todd432 2014-11-22 12:55:33

+0

那么如何让程序计算文件中的记录数呢? – Todd432 2014-11-22 12:59:04

回答

0

这将读取其中的内容到一个集合,你可以得到从收集的记录数。

Sub Main() 

    Dim FileName As String 
    Dim NumberOfRecords As Integer 
    FileName = "N:\2_7_towns(2).csv" 

    'read the lines into an array 
    Dim lines As String() = System.IO.File.ReadAllLines(FileName) 

    'read the array into a collection of town types 
    'this could also be done i a loop if you need better 
    'parsing or error handling 
    Dim TownList = From line In lines _ 
     Let data = line.Split(",") _ 
      Select New With {.Name = data(0), _ 
          .County = data(1), _ 
          .Population = data(2), _ 
          .Area = data(3)} 

    NumberOfRecords = TownList.Count 
    Console.WriteLine(NumberOfRecords) 
    Console.ReadLine() 

End Sub 

写到控制台会喜欢的东西来完成:

For Each town In TownList 
    Console.WriteLine(town.Name + "," + town.County) 
Next 
+0

你能解释从'Dim TownList'到'.Area = data(3)'的代码请 – Todd432 2014-11-22 13:12:01

+0

最重要的是选择新的部分 – Todd432 2014-11-22 13:22:55

+0

好的 - 这是一个LINQ查询线阵列它是说每行的行将数据分成逗号。然后选择每个新行到具有属性Name,County等的对象中,然后将该对象添加到TownList。 – Kevin 2014-11-22 13:23:14

0

很多方法可以做到这 测试此:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    dim FileName as string = "N:\2_7_towns(2).csv" 
    Dim Str() As String = System.IO.File.ReadAllLines(filename) 
    'Str(0) contains : "Town, County,Pop, Area" 
    'Str(1) contains : "Berwick-upon-tweed, Nothumberland,12870,468" 
    'Str(2) contains : "Bideford, devon,16262,430" 
    ' etc... 

    'Sample code for string searching : 
    Dim Lst As New List(Of String) 
    Lst.Add(Str(0)) 
    Dim LookingFor As String = "th" 
    For Each Line As String In Str 
     If Line.Contains(LookingFor) Then Lst.Add(Line) 
    Next 
    Dim Result As String = "" 
    For Each St As String In Lst 
     Result &= St & Environment.NewLine 
    Next 
    MessageBox.Show(Result) 

    'Sample code creating a grid : 
    Dim Grid = New DataGridView 
    Me.Controls.Add(Grid) 
    Grid.ColumnCount = Str(0).Split(","c).GetUpperBound(0) + 1 
    Grid.RowCount = Lst.Count - 1 
    Grid.RowHeadersVisible = False 

    For r As Integer = 0 To Lst.Count - 1 
     If r = 0 Then 
      For i As Integer = 0 To Lst(r).Split(","c).GetUpperBound(0) 
       Grid.Columns(i).HeaderCell.Value = Lst(0).Split(","c)(i) 
      Next 
     Else 
      For i As Integer = 0 To Lst(r).Split(","c).GetUpperBound(0) 
       Grid(i, r - 1).Value = Lst(r).Split(","c)(i) 
      Next 
     End If 

    Next 
    Grid.AutoResizeColumns() 
    Grid.AutoSize = True 
End Sub