Results 1 to 11 of 11

Thread: [RESOLVED] To know number of lines in a file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Resolved [RESOLVED] To know number of lines in a file

    I am usig a dat file in my project and I want to do some calculations base on the number of lines in the file so how can i know the number of lines in that file ?

  2. #2
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: To know number of lines in a file

    Explain how you reading that file...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: To know number of lines in a file

    You basically have no choice but to split the file contents into lines and count them. If you call File.ReadAllLines to get the data as lines already then simply get the Length of the resulting array. If you call File.ReadAllText or the like to get the file contents as a single String then you need to split that String into an array first, e.g.
    vb.net Code:
    1. Dim fileContents = File.ReadAllText(filePath)
    2. Dim lineCount = fileContents.Split({ControlChars.CrLf, ControlChars.Lf}, StringSplitOptions.None)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: To know number of lines in a file

    this is how i want to read it and then put the values inside a datagrid view
    Code:
    Public Sub Open()
            'show the open file dialog
            If MainForm.OpenFileDialog.ShowDialog = DialogResult.OK Then
                'Create a new sheet 
                Dim Nsheet As New ScoresheetForm
                Nsheet.MdiParent = MainForm
                'Call a new streamreader
                Dim reader As New StreamReader(MainForm.OpenFileDialog.FileName, False)
                'Filter out the commas or any other character separator
                Dim filterOut As Char() = ("?':;.,!""").ToCharArray()
                Do While Not reader.EndOfStream()
                    'Read each line as a record
                    Dim record As String = reader.ReadLine()
                    'Seperate all the data items
                    Dim data() As String = record.Split(" "c)
                    'Populate the datagrid view with the records
                    For a As Integer = 0 To Number of lines in file 
                        Nsheet.dgv.Rows.RowCount='Number of lines in file 
                        Nsheet.dgv.Rows(a).Cells(0).Value() = data(0)
                        Nsheet.dgv.Rows(a).Cells(0).Value() = data(1)
                        Nsheet.dgv.Rows(a).Cells(0).Value() = data(2)
                        Nsheet.dgv.Rows(a).Cells(0).Value() = data(3)
                        Nsheet.dgv.Rows(a).Cells(0).Value() = data(4)
                        Nsheet.dgv.Rows(a).Cells(0).Value() = data(5)
                        Nsheet.dgv.Rows(a).Cells(0).Value() = data(6)
                        Nsheet.dgv.Rows(a).Cells(0).Value() = data(7)
                        Nsheet.dgv.Rows(a).Cells(0).Value() = data(8)
                    Next
                Loop
                'Set the name and text property to its filename
                ActiveSheet.Text = MainForm.SaveFileDialog.FileName
                ActiveSheet.Name = MainForm.SaveFileDialog.FileName
                Nsheet.Show()
                reader.Dispose()
            End If
        End Sub

  5. #5
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: To know number of lines in a file

    To get lines counts jmc just give the answer.
    If what you actualy want is rows count

    Code:
    Dagridview.Rows.Count()
    Is what you need.
    But keep in mind you that if datagridview alows adding rows you need to subtract 1
    Last edited by Mike Storm; Sep 17th, 2017 at 07:36 PM.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: To know number of lines in a file

    no thats not what i mean. i am opening the dat file for reading, eah line is a record, the data items are seperated by commas. so it will be read into the data array.so i want to add rows accrding to the number of lines in the file

  7. #7
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: To know number of lines in a file

    Then Jmc just give you the answer

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: To know number of lines in a file

    You can't know how many lines are in the file without reading the file. Your code is reading the file line by line and processing each line as it's read. It's simply not possible to know how many lines there are before that process without reading the file twice. You have basically two choices:

    1. Read the file into an array first, get the Length of that array and then process the array elements instead of the lines of the file.
    2. Process the file as you are and simply increment a variable on each iteration of the loop, then use the variable as the count when you're done processing.

    There's no magic solution to this. Programming doesn't exist in a vacuum. Just as you can't magically get the number of items in a list without going through the list outside of programming, so you can't inside either.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: To know number of lines in a file

    i dont understand what you mean

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: To know number of lines in a file

    oh sorry i didn't see jmc's post my network is ceasing

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    257

    Re: To know number of lines in a file

    oh Jmc i got what you mean thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width