omit blank lines in file using StreamReader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWFuanJlZSBHYXJn?=

    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.

  • Rob Levine

    #2
    Re: omit blank lines in file using StreamReader

    15/04/2008 11:19:47
    Manjree Garg <garg@newsgroup .nospamwrote in message
    <CEB79234-3951-4A1C-B15B-6087F00748DF@mi crosoft.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

    rob@rob123levin e.removenumbers andme.co.uk

    Comment

    • Marc Gravell

      #3
      Re: omit blank lines in file using StreamReader

      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;
      }
      }
      }
      }

      Comment

      • Marc Gravell

        #4
        Re: omit blank lines in file using StreamReader

        "interator block"
        stupid fingers... iterator block

        Comment

        • =?Utf-8?B?TWFuanJlZSBHYXJn?=

          #5
          Re: omit blank lines in file using StreamReader

          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;
          }
          }
          }
          }
          >

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: omit blank lines in file using StreamReader

            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

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: omit blank lines in file using StreamReader

              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

              Comment

              • Jeffrey Tan[MSFT]

                #8
                Re: omit blank lines in file using StreamReader

                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:
                msdnmg@microsof t.com.

                =============== =============== =============== =====
                Get notification to my posts through email? Please refer to
                Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

                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.

                Comment

                Working...