2014-11-22 202 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