473,397 Members | 1,974 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,397 software developers and data experts.

StreamReader Help

I have the following code:

Dim oFile As Stream

Dim oReader As StreamReader

Dim strCarName

oFile =
[Assembly].GetExecutingAssembly.GetManifestResourceStream("A ssignment3.Vehicles.txt")

oReader = New StreamReader(oFile)

For Each strCarName In oReader.ReadLine

lstCarSpecs.Items.Add(strCarName)

Next

When it reads the text file it reads only the 1st line and displays it
as follows:

B
o
n
n
e
v
i
l
l
e

I thought the readline would read until a carriage return.

Help??

Melanie

Nov 23 '05 #1
6 1553
Hi TechieMom,
I thought the readline would read until a carriage return.

Yes, that is right. And you iterate through that line and print each
character on screen. The result is what expected.

If you want to read all lines and print each line on screen, use
something like following:
strCarName = oReader.ReadLine()
Do Until strCarName Is Nothing
Console.WriteLine(strCarName)
strCarName = oReader.ReadLine()
Loop

Regards,
Thi

Nov 23 '05 #2
Melanie,

And what is wrong with this?

The program has now probably assumed that "strCarName" is from the Type
Char, what would be the most normal in this instruction.
For Each strCarName In oReader.ReadLine
lstCarSpecs.Items.Add(strCarName)
Next


To prevent this, set option Strict On in top of your program,

I hope this helps,

Cor
Nov 23 '05 #3
"TechieMom" <me********@gmail.com> schrieb:
Dim oFile As Stream

Dim oReader As StreamReader

Dim strCarName

oFile =
[Assembly].GetExecutingAssembly.GetManifestResourceStream("A ssignment3.Vehicles.txt")

oReader = New StreamReader(oFile)

For Each strCarName In oReader.ReadLine

lstCarSpecs.Items.Add(strCarName)

Next


The problem with your code is that you are looping through the first line's
characters instead of looping through the lines:

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 23 '05 #4
Melanie,

What you wanted to do was probably
oReader = New StreamReader(oFile) dim myfile as string = oReader.ReadToEnd
For Each strCarName as String In myFile lstCarSpecs.Items.Add(strCarName)
Next


I hope this helps,

Cor

Nov 23 '05 #5
"TechieMom" <me********@gmail.com> wrote in message news:11**********************@g44g2000cwa.googlegr oups.com...
I have the following code:

Dim oFile As Stream
Dim oReader As StreamReader
Dim strCarName
oFile = [Assembly].GetExecutingAssembly.GetManifestResourceStream("A ssignment3.Vehicles.txt")
oReader = New StreamReader(oFile)
For Each strCarName In oReader.ReadLine
lstCarSpecs.Items.Add(strCarName)
Next

When it reads the text file it reads only the 1st line and displays it
as follows:

B
o
n
n
e
v
i
l
l
e

I thought the readline would read until a carriage return.


It does, but you're treating ReadLine as if it returns a
collection of strings. It doesn't, it returns just one string.
And because you haven't specified the type of strCarName
it's being treating as a Char type since a string is collection
of Chars--so you're effectively looping through each
character of the one string.

Also, remember to initialise all variables before using
them. Normally you'd initialise them to Nothing.

Finally, file processing should employ structured
error-trapping (Try..Catch...Finally).

Amend your code as follows:

' Initialise variables
Dim oFile As Stream = Nothing
Dim oReader As StreamReader = Nothing
Dim strCarName As String = Nothing

Try
' Open streams -- may cause exception
oFile = [Assembly].GetExecutingAssembly.GetManifestResourceStream("A ssignment3.Vehicles.txt")
oReader = New StreamReader(oFile)

' If you get this far, the streams are open
' and can be processed.

' Peek the next character in the file (don't
' process it). If it's (-1) you've reached the
' end of the file, otherwise keep reading
' strings.
While oReader.Peak > (-1)
strCarName = oReader.ReadLine
lstCarSpecs.Items.Add(strCarName)
End While
Catch Ex as System.Exception
' Handle errors
' E.g, display a friendly message.
Finally
strCarName = Nothing
End Try

Try
' oReader may not be open
oReader.Close()
Catch Ex As System.Exception
' Ignore error
Finally
oReader = Nothing
End Try

Try
' oFile may not be open
oFile.Close()
Catch Ex As System.Exception
' Ignore error
Finally
oFile = Nothing
End Try

' Note: The Finally clauses are optional.
' Garbage collection will take care of
' tidying up for you, but it's best to do
' your own cleanup. It's simply good
' programming etiquette.
Nov 23 '05 #6
Thanks everyone for your help! I was able to get it working correctly.
M

Nov 23 '05 #7

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

Similar topics

9
by: oafyuf | last post by:
Hi, I'm having performanbce issues with StreamReader and was wondering what I could do to improve it... The following takes around 3 seconds to process! The content of the response is: ...
3
by: redneon | last post by:
I have a program which is constantly reading from a stream and what I'm wanting to do is, if the stream hasn't sent anything after a certain amount of time then do something. I've tried doing this...
4
by: Astronomically Confused | last post by:
using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; class HttpProcessor { private Socket s;
8
by: WordVBAProgrammer | last post by:
I've been struggling with this for a few days now. It worked originally as plain VB-type strings, but for some reason ceased creating the FileNm. I changed the code to use StringBuilder, but...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
2
by: James Wong | last post by:
Dear all, I'm using StreamReader to read a text file containing BIG-5 data and found that no matter which encoding method in StreamReader's construction parameter, the BIG-5 contents become...
16
by: vvenk | last post by:
Hello: When I use either one to read a Text file, I get the same result. The length of the string that the file's content has been written into is the same. However, if the file is binary,...
4
by: KenLee | last post by:
help!! I used StreamReader and StreamWrite. the problem is it doesn't write all readline. For example it read 100 line and write 51lines. this is codes. class kenlee{ private StreamWriter sw...
5
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim...
3
by: =?Utf-8?B?Qm9zc2ll?= | last post by:
Hi All, I am having a little trouble with a StreamReader. I am currently reading a pipe delimited file with around 1.8 million records (total size 150MB) and, based on a flag, insert update or...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.