check for end of line

S

spowel4

I am using streamreader.readline to read in data from a file line by
line but I need to be able to check for the end of each line, how can
I accomplish that using streamreader.readline or is there a better
way?

My code thus far:

reader1 = File.OpenText(Application.StartupPath & "\filename")
While Not reader1.EndOfStream
fileContents = reader1.ReadLine
output = fileContents.Split(",")
While <condition to check for end of line>
------------------------------
 
G

Guest

well since your getting the file one line at a time and splitting it into an
array just check to see where you are in the array in the loop. if your at
the end of the array, youve reached the end of the line
 
N

News

spowel4 said:
I am using streamreader.readline to read in data from a file line by
line but I need to be able to check for the end of each line, how can
I accomplish that using streamreader.readline or is there a better
way?

My code thus far:

reader1 = File.OpenText(Application.StartupPath & "\filename")
While Not reader1.EndOfStream
fileContents = reader1.ReadLine
output = fileContents.Split(",")
While <condition to check for end of line>
------------------------------
------------------------------
End While
cnt = cnt + 1
End While
reader1.Close()

Here is a piece of code I use in a project that may help.

Dim fname As String = Application.StartupPath &
"\DataFiles\RawChanData\Channel" & s & ".txt"
Dim line As String = "", a() As String
If File.Exists(fname) Then
Static LastLine()
Using sr As StreamReader = New StreamReader(fname)
Do
line = sr.ReadLine()
If Not line = Nothing Then
a = Split(line, "~")
Process(line, LastLine, a)
LastLine = a
End If
Loop Until line Is Nothing
sr.Close()
LastLine = Nothing
End Using
Else
MsgBox("Channel List " & fname & " not found!")
End If
 
M

Michel Posseth [MCP]

or the right side of the columns so to speak

if you are splitting the lines in an array , then ofcourse the last position
of the array will contain the last part of the lines you have read in
you could check the last position while reading the upperbound of the array
 
T

Tom Shelton

I am using streamreader.readline to read in data from a file line by
line but I need to be able to check for the end of each line, how can
I accomplish that using streamreader.readline or is there a better
way?

My code thus far:

reader1 = File.OpenText(Application.StartupPath & "\filename")
While Not reader1.EndOfStream
fileContents = reader1.ReadLine
output = fileContents.Split(",")
While <condition to check for end of line>
------------------------------
------------------------------
End While
cnt = cnt + 1
End While
reader1.Close()

I'm not sure I understand the question? What do you mean by end-of-
line?

I imagine you want something like:
using reader as streamreader=new
streamreader(path.combine(application.startuppath, filename)
dim line as string = reader.readline()
while not line is nothing
dim fields() as string = line.split(",")
for each field as string in fields
' do cool stuff
next
end while
end using

not sure though...
Tom Shelton
 
J

Jack Jackson

The code splits the results of StreamReader.ReadLine into an array.
Why isn't the end of line at the end of the array?
 
M

Michel Posseth [MCP]

AFAIK


You only receive the line without the environments line seperator ( this
might be a cariage return and linefeed or only a cariage return )

i guess that the streamreader internally uses the environments newline
character to determine when it should stop and return the contents and thus
so removes it from the output . same as if you use a split and you wil lose
your split characters

michel


Jack Jackson said:
The code splits the results of StreamReader.ReadLine into an array.
Why isn't the end of line at the end of the array?
 
S

spowel4

I'm not sure I understand the question? What do you mean by end-of-
line?

I imagine you want something like:
using reader as streamreader=new
streamreader(path.combine(application.startuppath, filename)
dim line as string = reader.readline()
while not line is nothing
dim fields() as string = line.split(",")
for each field as string in fields
' do cool stuff
next
end while
end using

not sure though...
Tom Shelton

Tom, thank you very much; your example seems to work for me. I needed
to feed the contents of a comma-delimited file into a two-dimensional
array and each time end of line was reached, begin a new indice (not
sure if that's the correct term) within the array. In other words the
array would have elements (0,0....0,7) then at the end of line would
start array elements (1,0....1,7) and so on. I'm sure I'm not using
the correct terminology; sorry for that, I'm new to programming.
 
C

Cor Ligthert[MVP]

spowe14,

Beside all answers, be aware that there can be a difference between a
fysical textfile (what you calls a line) and that what Visual Studio is
using.

Visual is not giving information back after a 0 value in a row. (real 0 not
ASCII).

The file however can be longer.

Cor
 
T

Tom Shelton

Tom, thank you very much; your example seems to work for me. I needed
to feed the contents of a comma-delimited file into a two-dimensional
array and each time end of line was reached, begin a new indice (not
sure if that's the correct term) within the array. In other words the
array would have elements (0,0....0,7) then at the end of line would
start array elements (1,0....1,7) and so on. I'm sure I'm not using
the correct terminology; sorry for that, I'm new to programming.- Hide quoted text -

- Show quoted text -

Not sure how :) there is a serious but in that air code... it will
loop forever and only read one line. It really should look like

using reader as streamreader=new
streamreader(path.combine(application.startuppath, filename)
dim line as string = reader.readline()
while not line is nothing
dim fields() as string = line.split(",")
for each field as string in fields
' do cool stuff
next
line = reader.readline() ' forgot this!
end while
end using
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top