Results 1 to 14 of 14

Thread: [RESOLVED] number of ControlChars.NewLine

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Resolved [RESOLVED] number of ControlChars.NewLine

    How can I count the number of ControlChars.NewLine in my text file?

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Re: number of ControlChars.NewLine

    I figured out two way to count the lines in the text file:

    Dim lines As String() = IO.File.ReadAllLines("Survey.txt")
    'MessageBox.Show("The file had " & lines.Length & " lines.")

    And

    Dim count As Integer

    count = 0

    Dim obj As StreamReader

    obj = New StreamReader("Survey.txt")

    Do Until obj.ReadLine Is Nothing

    count = count + 1

    Loop
    obj.Close()
    MessageBox.Show(count)

    But how can I check how many records have been entered into the text file? I start a new record and separate them each with a new line (ControlChars.NewLine). Any help?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Re: number of ControlChars.NewLine

    Is there a way I could do a lop to check for the blank lines?

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: number of ControlChars.NewLine

    what exactly you want to accomplish?

    you want to know how many records the file has ? what is the format of the file? for each record there is one blank line? like so:

    First Record
    [blank line]
    Second Record
    [blank line]
    etc ...?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Re: number of ControlChars.NewLine

    Yes, each record has a blank line separating them and i need to get the number of records. And it's just a text file.

  6. #6
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: number of ControlChars.NewLine

    so correct me if i'm wrong, if each record is actually two lines and you have the number of line so this:

    Code:
    Dim lines As String() = IO.File.ReadAllLines("Survey.txt")
    'MessageBox.Show("The file had " & lines.Length & " lines.")
    
    And
    
    Dim count As Integer
    
    count = 0
    
    Dim obj As StreamReader
    
    obj = New StreamReader("Survey.txt")
    
    Do Until obj.ReadLine Is Nothing
    
    count = count + 1
    
    Loop
    obj.Close()
    Dim NumberOfLines As Integer = count / 2
    MessageBox.Show(NumberOfLines)
    gives you the number of records?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Re: number of ControlChars.NewLine

    Hmmm...I didn't think of it that way...That wold probably work great. Thanks! I will try it and see what I get!

  8. #8
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: number of ControlChars.NewLine

    use a For loop and have a counter variable increment by 1 with each line read. at the end of the loop, divide the counter variable by 2 and round down.

    remember to use the backslash for the division "\".

    lineCount \ 2 = numLineFeeds

  9. #9
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: number of ControlChars.NewLine

    @stateofidleness what is the different between using the "/" to "\" when using division ?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Re: number of ControlChars.NewLine

    It worked! Thanks so much for your help!!!

  11. #11
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] number of ControlChars.NewLine

    Glad i could help

    Please mark thread as resolved.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  12. #12
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: [RESOLVED] number of ControlChars.NewLine

    Division Operator '\, /'

    There are two division operations, one of them is a forward slash '/' and the other is a backward slash '\'. The difference between the two is that the forward-slash division operator '/' will result in a floating point (Double) while the backward slash '\' will result in a integer.

    Example:

    15 / 4 = 3.75
    15 \ 4 = 3


    Motil's code is great, if the end result is an even number of lines. If there are 5 lines and you divide by 2, your number of line feeds will be 2.5. Not exactly what you're after. The backslash will return an integer (5\2 = 2) which would mean 2 line feeds:

    0: Line of text 1
    1: linefeed
    2: Line of text 2
    3: linefeed
    4: Line of text 3

    5 lines. divide it by 2 with "\" = 2 linefeeds

  13. #13
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] number of ControlChars.NewLine

    Thanks for the info about the backslash stateofidlenes that's new to me.

    well if the format is always like to OP says it is (Record + Empty line) then he never should have uneven number of lines.

    Thanks again for the info.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  14. #14
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: [RESOLVED] number of ControlChars.NewLine

    Wouldn't it be better to get rid of the empty entries since the exact file format is unknown?

    Code:
            Dim validRecords As String() = _
                IO.File.ReadAllText("theFile.txt").Split({ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
            Dim numRecords As Integer = validRecords.Length

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