|
-
Apr 5th, 2010, 04:59 PM
#1
Thread Starter
Addicted Member
[RESOLVED] number of ControlChars.NewLine
How can I count the number of ControlChars.NewLine in my text file?
-
Apr 5th, 2010, 06:55 PM
#2
Thread Starter
Addicted Member
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?
-
Apr 5th, 2010, 07:00 PM
#3
Thread Starter
Addicted Member
Re: number of ControlChars.NewLine
Is there a way I could do a lop to check for the blank lines?
-
Apr 5th, 2010, 07:11 PM
#4
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 
-
Apr 5th, 2010, 07:34 PM
#5
Thread Starter
Addicted Member
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.
-
Apr 5th, 2010, 07:37 PM
#6
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 
-
Apr 5th, 2010, 07:42 PM
#7
Thread Starter
Addicted Member
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!
-
Apr 5th, 2010, 07:45 PM
#8
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
-
Apr 5th, 2010, 07:48 PM
#9
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 
-
Apr 5th, 2010, 07:50 PM
#10
Thread Starter
Addicted Member
Re: number of ControlChars.NewLine
It worked! Thanks so much for your help!!!
-
Apr 5th, 2010, 07:52 PM
#11
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 
-
Apr 5th, 2010, 08:00 PM
#12
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
-
Apr 6th, 2010, 03:06 AM
#13
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 
-
Apr 6th, 2010, 09:28 AM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|