473,396 Members | 2,014 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,396 software developers and data experts.

omit blank lines in file using StreamReader

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.

Jun 27 '08 #1
7 7717
15/04/2008 11:19:47
Manjree Garg <ga**@newsgroup.nospamwrote in message
<CE**********************************@microsoft.co m>
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.

--
Rob Levine
http://blog.roblevine.co.uk/
ro*@rob123levine.removenumbersandme.co.uk
Jun 27 '08 #2
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<stringReadNonEmptyLines(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;
}
}
}
}
Jun 27 '08 #3
"interator block"
stupid fingers... iterator block
Jun 27 '08 #4
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
"Marc Gravell" wrote:
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<stringReadNonEmptyLines(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;
}
}
}
}
Jun 27 '08 #5
On Apr 15, 4:05*am, Manjree Garg <g...@newsgroup.nospamwrote:
* *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
Jun 27 '08 #6
On Apr 15, 3:34*am, Marc Gravell <marc.grav...@gmail.comwrote:
"interator block"

stupid fingers... iterator block
On the other hand that would be a cool alias for an iterator block
returning IEnumerable<int>.

Jon
Jun 27 '08 #7
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #8

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

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
10
by: Craig Bumpstead | last post by:
Hi, I was wondering the best and fastest way to determine how many lines are in a log file. At the moment I am simply doing a StreamReader.ReadLine and incrementing a counter until I reach...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
4
by: Larry Woods | last post by:
I have a seq. file with some blank lines (CrLf only) in it. When the line is read, I get a value of "Nothing" returned, which give me an "End of File" condition, so I terminate my procedure. ...
4
by: Ryan S | last post by:
I am trying to read an XML document generated by a web server using the XMLTextReader class, but the document generated appears to have some blank lines at the top that are causing problems. If...
2
by: ReidarT | last post by:
I use this code to print a textfile, but it gives me a lot of blank pages. The file contains 9 lines of text. Private strFileName As String = "C:\Sendepost\LoggFil.txt" Private objStreamToPrint...
14
by: mesterak | last post by:
I want to very quickly count the number of lines in text files without having to read each line and increment a counter. I am working in VB.NET and C#. Does anyone have a very fast example on how...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
9
by: NvrBst | last post by:
Whats the best way to count the lines? I'm using the following code at the moment: public long GetNumberOfLines(string fileName) { int buffSize = 65536; int streamSize = 65536; long...
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?
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.