Read from file line by line

  • Thread starter Thread starter denislagoda
  • Start date Start date
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.
 
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
 
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
 
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");
}
}
 
Som Nath Shukla said:
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)
{
...
}
 
....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
 
Martijn said:
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
 
Hilton said:
...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
 

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

Back
Top