473,732 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StreamReader Help

I have the following code:

Dim oFile As Stream

Dim oReader As StreamReader

Dim strCarName

oFile =
[Assembly].GetExecutingAs sembly.GetManif estResourceStre am("Assignment3 .Vehicles.txt")

oReader = New StreamReader(oF ile)

For Each strCarName In oReader.ReadLin e

lstCarSpecs.Ite ms.Add(strCarNa me)

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 1571
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.ReadLin e()
Do Until strCarName Is Nothing
Console.WriteLi ne(strCarName)
strCarName = oReader.ReadLin e()
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.ReadLin e
lstCarSpecs.Ite ms.Add(strCarNa me)
Next


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

I hope this helps,

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

Dim oReader As StreamReader

Dim strCarName

oFile =
[Assembly].GetExecutingAs sembly.GetManif estResourceStre am("Assignment3 .Vehicles.txt")

oReader = New StreamReader(oF ile)

For Each strCarName In oReader.ReadLin e

lstCarSpecs.Ite ms.Add(strCarNa me)

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&la ng=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(oF ile) dim myfile as string = oReader.ReadToE nd
For Each strCarName as String In myFile lstCarSpecs.Ite ms.Add(strCarNa me)
Next


I hope this helps,

Cor

Nov 23 '05 #5
"TechieMom" <me********@gma il.com> wrote in message news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
I have the following code:

Dim oFile As Stream
Dim oReader As StreamReader
Dim strCarName
oFile = [Assembly].GetExecutingAs sembly.GetManif estResourceStre am("Assignment3 .Vehicles.txt")
oReader = New StreamReader(oF ile)
For Each strCarName In oReader.ReadLin e
lstCarSpecs.Ite ms.Add(strCarNa me)
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...F inally).

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].GetExecutingAs sembly.GetManif estResourceStre am("Assignment3 .Vehicles.txt")
oReader = New StreamReader(oF ile)

' 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.ReadLin e
lstCarSpecs.Ite ms.Add(strCarNa me)
End While
Catch Ex as System.Exceptio n
' 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.Exceptio n
' Ignore error
Finally
oReader = Nothing
End Try

Try
' oFile may not be open
oFile.Close()
Catch Ex As System.Exceptio n
' 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
12761
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: "<?xml version="1.0" ?><ERROR>ORA-01403: no data found</ERROR>" HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strURIQuery);
3
2145
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 like this... while(!done) { idleTimer.Start(); response = streamReader.ReadLine(); idleTimer.Stop(); ....
4
8745
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
5835
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 only get the file name, not the path. Can someone point out my error? See notes below code. .... Dim strFileNm As String Dim FileNm As New StringBuilder()
7
7771
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 strReturn += input + vbCrLf input = sr.Read
2
4191
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 garbage under ReadLine method. Does anybody have any idea on this issue? Thanks for your attention and kindly help! Regards,
16
2088
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, FileGet gets me the correct content while StreamReader gives me a truncated string. Can somebody advise me why? Should I be using BinaryReader instead of StreamReader? Would BinaryReader work on text files?
4
3134
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 ; private StreamReader sr; private String line;
5
2220
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 Import_File_Reader As System.IO.StreamReader While ...
3
544
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 delete a record in a SQL Server DB using System.Data.SqlClient. So I do a ReadLine() on the streamreader, do my action and then step to the next line. (Note I am using SqlBulkCopy to do the DB side, but I don't think that really affects by...
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.