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

more effective way of reading file with streamreader?

VMI
I normally use the following sequence of code to read from a file. Is there
any other way I can do it where I don't have to do that first ReadLine()
that's outside of the loop? But if I don't do that, how am I going to
process that first line?

myReader.Open();
myVar = myReader.ReadLine(); //for first line of file
while (myVar != null)
{
/* do what I have to do with myVar */
myVar = myReader.ReadLine();
}

Thanks.
Nov 16 '05 #1
8 1666
VMI <vo******@yahoo.com> wrote:
I normally use the following sequence of code to read from a file. Is there
any other way I can do it where I don't have to do that first ReadLine()
that's outside of the loop? But if I don't do that, how am I going to
process that first line?

myReader.Open();
myVar = myReader.ReadLine(); //for first line of file
while (myVar != null)
{
/* do what I have to do with myVar */
myVar = myReader.ReadLine();
}


My normal code is something like:

string line;

while ( (line=myReader.ReadLine()) != null)
{
// Do something with line
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi,

You could try

myReader.Open();

while ( (myVar = myReader.ReadLine()) != null)
{
/* do what I have to do with myVar */
}

Or maybe:

myReader.Open();
do
{
/* do what I have to do with myVar */
myVar = myReader.ReadLine();
} while (myVar != null);
Martin

VMI wrote:
I normally use the following sequence of code to read from a file. Is there
any other way I can do it where I don't have to do that first ReadLine()
that's outside of the loop? But if I don't do that, how am I going to
process that first line?

myReader.Open();
myVar = myReader.ReadLine(); //for first line of file
while (myVar != null)
{
/* do what I have to do with myVar */
myVar = myReader.ReadLine();
}

Thanks.

Nov 16 '05 #3
VMI
I didn't know that was possible. Thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
VMI <vo******@yahoo.com> wrote:
I normally use the following sequence of code to read from a file. Is
there
any other way I can do it where I don't have to do that first ReadLine()
that's outside of the loop? But if I don't do that, how am I going to
process that first line?

myReader.Open();
myVar = myReader.ReadLine(); //for first line of file
while (myVar != null)
{
/* do what I have to do with myVar */
myVar = myReader.ReadLine();
}


My normal code is something like:

string line;

while ( (line=myReader.ReadLine()) != null)
{
// Do something with line
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
VMI <vo******@yahoo.com> wrote:
I didn't know that was possible. Thanks.


Just bear in mind that *usually* it's a really bad idea to include
assignment in the middle of something else. However, it's a common
idiom in this case - not just for reading lines, but also reading
chunks of data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
"Martin" <ma**********************@eircom.NOSPAM.net> wrote in message news:Or**************@TK2MSFTNGP14.phx.gbl...
myReader.Open();
do
{
/* do what I have to do with myVar */
myVar = myReader.ReadLine();
} while (myVar != null);


Except that where you have "/* do what I have to do with myVar */" - myVar at that point has no value (at least, not one that
has been read from the file).

--
Adam Clauss
ca*****@tamu.edu

Nov 16 '05 #6
Thats the problem with cut & paste :-) I pasted the comment in the wrong
place. Sorry.

Martin

Adam Clauss wrote:
"Martin" <ma**********************@eircom.NOSPAM.net> wrote in message
news:Or**************@TK2MSFTNGP14.phx.gbl...
myReader.Open();
do
{
/* do what I have to do with myVar */
myVar = myReader.ReadLine();
} while (myVar != null);

Except that where you have "/* do what I have to do with myVar */"
- myVar at that point has no value (at least, not one that has been read
from the file).

Nov 16 '05 #7
Nope, still doesn't work... Move the comment to after the ReadLine(),
and on the last pass, you're working on a null string.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Martin" <ma**********************@eircom.NOSPAM.net> wrote in message
news:u1**************@TK2MSFTNGP11.phx.gbl...
Thats the problem with cut & paste :-) I pasted the comment in the wrong
place. Sorry.

Martin

Adam Clauss wrote:
"Martin" <ma**********************@eircom.NOSPAM.net> wrote in message
news:Or**************@TK2MSFTNGP14.phx.gbl...
myReader.Open();
do
{
/* do what I have to do with myVar */
myVar = myReader.ReadLine();
} while (myVar != null);

Except that where you have "/* do what I have to do with myVar */"
- myVar at that point has no value (at least, not one that has been read
from the file).

Nov 16 '05 #8
James Curran <Ja*********@mvps.org> wrote:
Nope, still doesn't work... Move the comment to after the ReadLine(),
and on the last pass, you're working on a null string.


myReader.Open();
while (true) {
MyVar myVar = myReader.ReadLine();
if (myVar == null) {
break;
}

// do something with myVar
}

Some might immediately frown at this because we're exiting from the loop
with 'break', but practically, it's simple and readable since it has only
one exit point.

A benefit of this construct is that the variable can be declared in the
inner-most block in which it's required.

Note that declaring the variable inside of the loop is just as performant
as declaring it outside of the loop.
Nov 16 '05 #9

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

Similar topics

2
by: Jessard | last post by:
Hi all, I am having trouble reading an XML document in and looping through it's nodes in VB.NET. The error is specific, it is: "There is no Unicode byte order mark. Cannot switch to Unicode."...
6
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
13
by: mloichate | last post by:
I must read a very heavy-weight text plain file (usually .txt extension) )and replace a given character with another given character in all text inside the file. My application was working pretty...
9
by: haibhoang | last post by:
I have a Windows Service that is trying to parse a large (> 1Gig) text file. I am keep getting OutOfMemoryException exception. Here is the code that's having problem: using (StreamReader...
9
by: jeff M via .NET 247 | last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I...
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...
6
by: belmontpress | last post by:
I wish to delete some files from a directory after reading them but have the problem that the system says that the files are in use and cannot delete them even though I have set the StreamReader to...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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...

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.