omit blank lines in file using StreamReader

M

Manjree Garg

Hi,


I am using StreamReader to read an ASCII file that contains blank lines.
How can I omit reading blank lines? I tried somting like...


FileStream inFile = new FileStream("c:\HTAC10A.PRN",FileMode.Open);
StreamReader inreader = new StreamReader(inFile);
string line = inreader.ReadLine();
if(line.Trim() != line.Empty)


but it did not work.

Any suggestions will be appriciated.
 
R

Rob Levine

15/04/2008 11:19:47
Hi,


I am using StreamReader to read an ASCII file that contains blank lines.
How can I omit reading blank lines? I tried somting like...


FileStream inFile = new FileStream("c:\HTAC10A.PRN",FileMode.Open);
StreamReader inreader = new StreamReader(inFile);
string line = inreader.ReadLine();
if(line.Trim() != line.Empty)


but it did not work.

Any suggestions will be appriciated.

try
if(line.Trim().Length > 0)
As a quick aside, make sure that you close the FileStream and
StreamReader correctly, even in the event of an exception being
raised.
 
M

Marc Gravell

That approach should (typos aside) work fine; see below - to make the
calling code simpler, I've moved the code that worries about reading
just non-empty lines into a separate method (using an "interator
block"):

static void Main()
{
foreach (string line in ReadNonEmptyLines(@"c:\foo.txt"))
{
Console.WriteLine(line);
}
}

static IEnumerable<string> ReadNonEmptyLines(string path)
{
using (TextReader reader = File.OpenText(path))
{
string line;
while ((line = reader.ReadLine()) != null) // EOF
{
line = line.Trim();
if (line != "")
{ // oinly report non-empty lines
yield return line;
}
}
}
}
 
M

Manjree Garg

Hi Marc,

Thanks for the suggestion. It is working with the text file that I
created by myself containing blak lines. But I have got Raman spectroscopy
data files that contain a blank line at the end and it shows an arrow (->) in
that line when I open it in word pad. It does not work with those files???


cheers.

Manjree
 
J

Jon Skeet [C# MVP]

   Thanks for the suggestion. It is working with the text file that I
created by myself containing blak lines. But I have got Raman spectroscopy
data files that contain a blank line at the end and it shows an arrow (->)in
that line when I open it in word pad. It does not work with those files???

Look at the file in a hex editor - they've probably got some odd line
ending.

Jon
 
J

Jeffrey Tan[MSFT]

Hi Manjree,

Yes, I agree with Jon that that line may not be empty. There may be
invisible or non-Ascii characters in that line which are shown as "??" in
text editor. Using a binary hex editor such as Visual Studio will give you
the actual encoding of that line.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top