[RESOLVED] StreamReader.ReadLine() ERROR!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] StreamReader.ReadLine() ERROR!

  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [RESOLVED] StreamReader.ReadLine() ERROR!

    I am reading values from a selected textfile and then displaying them in a textbox present on my Windows.Form. After my whole textfile has been looped through, the last line in my textfile is repeated again and then I receive the error message: "Exception: Index out of array range."

    Do you know what I could have done wrong in my loop?

    Code:
            protected void ReadFile()
            {
                try
                {
                    String line = null;
                    using (mStreamReader = File.OpenText(mOpenFileDialog.FileName))
                    {
                        while ((line = mStreamReader.ReadLine()) != null)
                        {
                            SplitLine(line);
                            ConvertToLong();
                        }
                    }
                    tbxDisplay.Text = msContents;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: StreamReader.ReadLine() ERROR!

    is there a blank line at the end of your file?
    ===============================
    My Blog

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: StreamReader.ReadLine() ERROR!

    And what are the SplitLine() and ConverToLong() methods doing?

  4. #4
    Join Date
    Jan 2010
    Posts
    130

    Re: StreamReader.ReadLine() ERROR!

    Amazing, I took away the two blank lines at the end of my file and it worked! How do I prevent this from happening though without manually taking away the blank lines?

  5. #5
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: StreamReader.ReadLine() ERROR!

    change
    Code:
    while ((line = mStreamReader.ReadLine()) != null)
    {
      SplitLine(line);
      ConvertToLong();
    }
    into
    Code:
    while ((line = mStreamReader.ReadLine()) != null)
    {
      if (string.IsNullOrEmpty(line))
        break;
      SplitLine(line);
      ConvertToLong();
    }
    But this will also break the operation if there is an blank line in the middle of the file. You could also ignore blank lines
    Code:
    while ((line = mStreamReader.ReadLine()) != null)
    {
      if (!string.IsNullOrEmpty(line)) //only execute if line is not empty
      {
        SplitLine(line);
        ConvertToLong();
      }
    }

  6. #6
    Join Date
    Jan 2010
    Posts
    130

    Talking Re: StreamReader.ReadLine() ERROR!

    Works perfectly!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center