469,270 Members | 1,149 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Read from file line by line

I am a beginer.

I need to read a TXT file line by line

How to do it?

Sep 28 '07 #1
17 24350
On Sep 28, 5:23 am, denislag...@gmail.com wrote:
I am a beginer.

I need to read a TXT file line by line

How to do it?
Look up the StreamReader and it's ReadLine method.

Thanks,

Seth Rowe

Sep 28 '07 #2
On Sep 28, 5:23 am, denislag...@gmail.com wrote:
I am a beginer.

I need to read a TXT file line by line

How to do it?
Hi, try something like this:

static void Main()
{
FileStream fileStream = null;
StreamReader streamReader = null;
try
{
fileStream = new FileStream(@"c:\foo.txt", FileMode.Open);
streamReader = new StreamReader(fileStream);
while (true)
{
string line = streamReader.ReadLine();
if (string.IsNullOrEmpty(line))
break;
}
}
finally
{
if (streamReader != null)
streamReader.Close();
if (fileStream != null)
fileStream.Close();
}
}

Hope it helps,
John

Sep 28 '07 #3
StreamReader reader = new StreamReader(filePath);//filepatj="C:/aa.txt"
while (!reader.EndOfStream)
{
InputFile = reader.ReadLine();
// do what u want

}
this ReadLine() fuctionread the line by line and return one line at a time
as string.
"de*********@gmail.com" wrote:
I am a beginer.

I need to read a TXT file line by line

How to do it?

Sep 28 '07 #4
On 28 , 13:28, Som Nath Shukla
<SomNathShu...@discussions.microsoft.comwrote:
StreamReader reader = new StreamReader(filePath);//filepatj="C:/aa.txt"
while (!reader.EndOfStream)
{
InputFile = reader.ReadLine();
// do what u want

}
StreamReader.EndOfStream - didnt find in MSDN

Sep 28 '07 #5
I need to read a TXT file line by line

using System;
using System.IO;
class TextFileExplorer
{
TextFileExplorer(string filename)
{
try
{
StreamReader streamreader=new
FileInfo(filename).OpenText();
string text=streamreader.ReadLine();
while(text!=null)
{
Console.WriteLine(text);
text=streamreader.ReadLine();
}
streamreader.Close();
}
catch
{
Console.WriteLine("File {0} does not exist",filename);
}
}
static void Main()
{
new TextFileExplorer(@"C:\somefile.txt");
}
}

Sep 28 '07 #6
use system.IO namespace
as
using system.IO
it will work

"de*********@gmail.com" wrote:
On 28 , 13:28, Som Nath Shukla
<SomNathShu...@discussions.microsoft.comwrote:
StreamReader reader = new StreamReader(filePath);//filepatj="C:/aa.txt"
while (!reader.EndOfStream)
{
InputFile = reader.ReadLine();
// do what u want

}

StreamReader.EndOfStream - didnt find in MSDN

Sep 28 '07 #7
http://msdn.microsoft.com/library/ru...mbersTopic.asp

? where EndOfStream

On 28 , 15:07, Som Nath Shukla
<SomNathShu...@discussions.microsoft.comwrote:
use system.IO namespace
as
using system.IO
it will work

"denislag...@gmail.com" wrote:
On 28 , 13:28, Som Nath Shukla
<SomNathShu...@discussions.microsoft.comwrote:
StreamReader reader = new StreamReader(filePath);//filepatj="C:/aa.txt"
while (!reader.EndOfStream)
{
InputFile = reader.ReadLine();
// do what u want
}
StreamReader.EndOfStream - didnt find in MSDN- -

- -

Sep 28 '07 #8
Where MSDN for framework 2.0 ?

Sep 28 '07 #9

http://msdn2.microsoft.com/en-us/lib...dofstream.aspx
use this one endofstream is property of streamreader .
use it dont worry.
"de*********@gmail.com" wrote:
http://msdn.microsoft.com/library/ru...mbersTopic.asp

? where EndOfStream

On 28 , 15:07, Som Nath Shukla
<SomNathShu...@discussions.microsoft.comwrote:
use system.IO namespace
as
using system.IO
it will work

"denislag...@gmail.com" wrote:
On 28 , 13:28, Som Nath Shukla
<SomNathShu...@discussions.microsoft.comwrote:
StreamReader reader = new StreamReader(filePath);//filepatj="C:/aa.txt"
while (!reader.EndOfStream)
{
InputFile = reader.ReadLine();
// do what u want
}
StreamReader.EndOfStream - didnt find in MSDN- -
- -


Sep 28 '07 #10
this propert is new .net 2.0

"de*********@gmail.com" wrote:
http://msdn.microsoft.com/library/ru...mbersTopic.asp

? where EndOfStream

On 28 , 15:07, Som Nath Shukla
<SomNathShu...@discussions.microsoft.comwrote:
use system.IO namespace
as
using system.IO
it will work

"denislag...@gmail.com" wrote:
On 28 , 13:28, Som Nath Shukla
<SomNathShu...@discussions.microsoft.comwrote:
StreamReader reader = new StreamReader(filePath);//filepatj="C:/aa.txt"
while (!reader.EndOfStream)
{
InputFile = reader.ReadLine();
// do what u want
}
StreamReader.EndOfStream - didnt find in MSDN- -
- -


Sep 28 '07 #11
click on link u will get it

"Som Nath Shukla" wrote:
>
http://msdn2.microsoft.com/en-us/lib...dofstream.aspx
use this one endofstream is property of streamreader .
use it dont worry.
"de*********@gmail.com" wrote:
http://msdn.microsoft.com/library/ru...mbersTopic.asp

? where EndOfStream

On 28 , 15:07, Som Nath Shukla
<SomNathShu...@discussions.microsoft.comwrote:
use system.IO namespace
as
using system.IO
it will work
>
>
>
"denislag...@gmail.com" wrote:
On 28 , 13:28, Som Nath Shukla
<SomNathShu...@discussions.microsoft.comwrote:
StreamReader reader = new StreamReader(filePath);//filepatj="C:/aa.txt"
while (!reader.EndOfStream)
{
InputFile = reader.ReadLine();
// do what u want
>
}
>
StreamReader.EndOfStream - didnt find in MSDN- -
>
- -
Sep 28 '07 #12
http://msdn2.microsoft.com/

"de*********@gmail.com" wrote:
Where MSDN for framework 2.0 ?

Sep 28 '07 #13
de*********@gmail.com wrote:
StreamReader.EndOfStream - didnt find in MSDN
Why not? Are you looking at the .NET 1.1 docs?

http://msdn2.microsoft.com/en-us/lib...dofstream.aspx
Sep 28 '07 #14
Som Nath Shukla <So***********@discussions.microsoft.comwrote:
StreamReader reader = new StreamReader(filePath);//filepatj="C:/aa.txt"
while (!reader.EndOfStream)
{
InputFile = reader.ReadLine();
// do what u want

}
this ReadLine() fuctionread the line by line and return one line at a time
as string.
It's better (IMO) to read until ReadLine returns null. There's no need
to have a separate "test then read":

string line;

while ( (line=reader.ReadLine()) != null)
{
...
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 28 '07 #15
....or simply (and more correctly):

using (StreamReader sr = new StreamReader (filename))
{
string line;
while ((line = sr.ReadLine()) != null)
{
do stuff
}
}

Works with all versions of .NET and CF (to the best of my knowledge)

Hilton

"John Duval" <Jo********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On Sep 28, 5:23 am, denislag...@gmail.com wrote:
>I am a beginer.

I need to read a TXT file line by line

How to do it?

Hi, try something like this:

static void Main()
{
FileStream fileStream = null;
StreamReader streamReader = null;
try
{
fileStream = new FileStream(@"c:\foo.txt", FileMode.Open);
streamReader = new StreamReader(fileStream);
while (true)
{
string line = streamReader.ReadLine();
if (string.IsNullOrEmpty(line))
break;
}
}
finally
{
if (streamReader != null)
streamReader.Close();
if (fileStream != null)
fileStream.Close();
}
}

Hope it helps,
John

Sep 29 '07 #16
Martijn Mulder wrote:
StreamReader streamreader=new
FileInfo(filename).OpenText();
There are a StreamReader constructor that takes a filename as argument.
string text=streamreader.ReadLine();
while(text!=null)
{
Console.WriteLine(text);
text=streamreader.ReadLine();
}
The style:

String line;
while((line = sr.ReadLine()) != null)
{
...
}

is in many ways the standard way of doing it and by using
it you will make it easier for other to read your code.

Arne
Oct 2 '07 #17
Hilton wrote:
"John Duval" <Jo********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
>static void Main()
{
FileStream fileStream = null;
StreamReader streamReader = null;
try
{
fileStream = new FileStream(@"c:\foo.txt", FileMode.Open);
streamReader = new StreamReader(fileStream);
while (true)
{
string line = streamReader.ReadLine();
if (string.IsNullOrEmpty(line))
break;
}
}
finally
{
if (streamReader != null)
streamReader.Close();
if (fileStream != null)
fileStream.Close();
}
}
...or simply (and more correctly):

using (StreamReader sr = new StreamReader (filename))
{
string line;
while ((line = sr.ReadLine()) != null)
{
do stuff
}
}
Not only does it save a lot of source code and is much more
readable.

It does not abort reading when reaching an empty line, which
there are no indication that the original poster wanted.

Arne
Oct 2 '07 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

40 posts views Thread by Abby | last post: by
3 posts views Thread by juli jul | last post: by
35 posts views Thread by RyanS09 | last post: by
11 posts views Thread by jo3c | last post: by
3 posts views Thread by George | last post: by
reply views Thread by leeamiin | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.