473,788 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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",Fi leMode.Open);
StreamReader inreader = new StreamReader(in File);
string line = inreader.ReadLi ne();
if(line.Trim() != line.Empty)
but it did not work.

Any suggestions will be appriciated.

Jun 27 '08 #1
7 7775
15/04/2008 11:19:47
Manjree Garg <ga**@newsgroup .nospamwrote in message
<CE************ *************** *******@microso ft.com>
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",Fi leMode.Open);
StreamReader inreader = new StreamReader(in File);
string line = inreader.ReadLi ne();
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*@rob123levin e.removenumbers andme.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 ReadNonEmptyLin es(@"c:\foo.txt "))
{
Console.WriteLi ne(line);
}
}

static IEnumerable<str ingReadNonEmpty Lines(string path)
{
using (TextReader reader = File.OpenText(p ath))
{
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 ReadNonEmptyLin es(@"c:\foo.txt "))
{
Console.WriteLi ne(line);
}
}

static IEnumerable<str ingReadNonEmpty Lines(string path)
{
using (TextReader reader = File.OpenText(p ath))
{
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...@g mail.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****@microsof t.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
4301
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 while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and other with blanks. Thanks a lot.
10
7625
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 the end. Is there a better way?? Cheers, Craig
21
13101
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 server is on one line (\r\n at end) and is formed as - each of which is seperated by a space. Arguments with spaces in them are enclosed in quotations. So, I'm able to open a connection to the server. When I send a message to
4
3578
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. How do you get around interspersed blank lines? TIA, Larry Woods
4
3240
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 I connect directly to the URL, when I call the ".Read" method on the XMLTextReader, I get the message: "The XML declaration is unexpected. Line 8, Column 3" If I open the URL in my browser and copy and paste the XML stuff (starting
2
3354
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 As StreamReader Private objPrintFont As Font Private Sub cmdSkrivUt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSkrivUt.Click Dim objPrintDocument As PrintDocument = New PrintDocument
14
23276
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 to do this? Thanks, Matt
7
2117
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 lines. and after that save both of those files... since that is a big block of text (15000 lines) i thint that it is a big job to look for a keyword... so my question is this exactly: how do i do this:
9
13239
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 numOfLines = 0; byte bArr = new byte;
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10175
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.