473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

HOW do I count the Number of lines in a text file

I'm using the streamreader stuff. I know how to write and append, but I
don't know how to get VB to return the number of lines that there are in the
sequential access file that I'm working with.

Example:
I've written 40 lines to the file. I know that, but how do I get the
computer to tell me how many lines I have writte to the file? something
like io.file.linecount would be nice, but I'm not finding anything like that
anywhere.

Thanks
Nov 21 '05 #1
6 17411
"Daniel Kaseman" <da*@cfaith.com> schrieb:
I'm using the streamreader stuff. I know how to write and append, but I
don't know how to get VB to return the number of lines that there are in
the
sequential access file that I'm working with.


You'll either maintain a counter variable while writing lines to the file or
read the file line-by-line using a 'StreamReader' in order to determine the
number of lines.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
yes, but what is the syntax?

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ev****************@TK2MSFTNGP14.phx.gbl...
"Daniel Kaseman" <da*@cfaith.com> schrieb:
I'm using the streamreader stuff. I know how to write and append, but I
don't know how to get VB to return the number of lines that there are in
the
sequential access file that I'm working with.
You'll either maintain a counter variable while writing lines to the file

or read the file line-by-line using a 'StreamReader' in order to determine the number of lines.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
"Daniel Kaseman" <da*@cfaith.com> schrieb:
yes, but what is the syntax?


Reading a text file line-by-line or blockwise with a progress indicator
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4
I'm using the streamreader stuff. I know how to write and append, but I
don't know how to get VB to return the number of lines that there are in
the
sequential access file that I'm working with.
You cannot write and append with a streamreader
Example:
I've written 40 lines to the file. I know that, but how do I get the
computer to tell me how many lines I have writte to the file?
Where is the file that you have readed, in memory?
something like io.file.linecount would be nice, but I'm not finding
anything like that
anywhere.

This I don't understand absolute not, what should it do, probably is the
file that you have readed in a kind of collection. You can get the
count/length from it.

Cor
Nov 21 '05 #5
In addition, if all the records are the same length, then you can get
the number of lines by dividing the file size by the record length.

Nov 21 '05 #6
There are two ways I would do this:

1)

Dim intLines As Integer = TextBox1.Lines.Length
MessageBox.Show(intLines, Me.Text)

2)

TextBox1.Text = ""
With OpenFileDialog1
.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolde r.Desktop)
.Filter = "Text Files (*.txt)|*.txt"
If .ShowDialog <> DialogResult.OK Then Exit Sub
End With
Dim intLines As Integer = 0
Dim sr As New IO.StreamReader(OpenFileDialog1.FileName)
Do While sr.Peek() >= 0
TextBox1.Text += sr.ReadLine() & ControlChars.CrLf
intLines += 1
Loop
sr.Close()
MessageBox.Show(intLines, Me.Text)

I hope this helps

Crouchie1998
BA (HONS) MCP MCSE
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
3
by: Bruce | last post by:
Is there any mechanism in vs 2005 to have the IDE report the number of lines of source code in a C# project either per file or preferably across an entire project? Thanks, Bruce
14
by: mesterak | last post by:
I want to very quickly count the number of lines in text files without having to read each line and increment a counter. I am working in VB.NET and C#. Does anyone have a very fast example on how...
4
by: gualtmacchi | last post by:
I'm processing an XML input file getting a plain text file where from M nodes I got N output lines... It's not relevant but the input file is a recordset coming from a database and the output is...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
3
by: javanooby | last post by:
Hi, I am having problems with this bit of code: public class main { public class readAccounts { reader1 r = new reader1();
8
by: shivam001 | last post by:
I have the following file as the input APPLE 0 118 1 110 1 125 1 135 2 110 3 107 3 115 3 126 ORANGE 0 112 1 119 2 109 2 119 3 112 4 109 4 128 MANGO 0 136 1 143 2 143 3 143 4 136 BANANA 0 5 1...
13
by: humaid | last post by:
hi,guys i have done a program to count the number of bigrams. i have taken a input file by using @ARGV,then icounted the number of lines in the file,using the split function i splited the sentence...
3
by: waynejr25 | last post by:
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.