reading a .txt file and displaying numbers from the file

0

I am trying to read a .txt file using c# and displaying its contents but I am getting error as IndexOutOfRangeException with error code as 0xc000013a.

Here's my code:

static void Main(string[] args)
    {
        StreamReader sStreamReader = new StreamReader("d:\\TEST.txt");
        while (!sStreamReader.EndOfStream)
        {
            string sLine = "";
            if (sLine != null)
            {
                sLine = sStreamReader.ReadLine();
                if (sLine != null)
                {
                    string[] rows = sLine.Split(",".ToCharArray());
                    double a = Convert.ToDouble(rows[1]);
                    Console.Write(a);
                    int b = Convert.ToInt32(rows[3]);
                    Console.WriteLine(b);
                    Console.WriteLine();
                }
            }
        }
    }

my text file is as follows:

1,2,3,4,5,6,7

1,2,3,4,5,6,7

5,6,2,7,3,8,4

3,4,3,4,3

5,3,23,12

12,30000,12,99
c#
file
asked on Stack Overflow Aug 3, 2012 by Aditya Agarwal • edited Aug 3, 2012 by Deqing

4 Answers

2

I would change it to the following:

static void Main(string[] args)
    {
        // StreamReader is IDisposable which should be wrapped in a using statement
        using (StreamReader reader = new StreamReader(@"d:\TEST.txt")) 
        {
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                // make sure we have something to work with
                if (String.IsNullOrEmpty(line)) continue;

                string[] cols = line.Split(',');
                // make sure we have the minimum number of columns to process
                if (cols.Length < 4) continue;

                double a = Convert.ToDouble(cols[1]);
                Console.Write(a);
                int b = Convert.ToInt32(cols[3]);
                Console.WriteLine(b);
                Console.WriteLine();
            }
        }
    }

Some notes here:

  1. StreamReader implements IDisposable, so you should wrap it in a using clause so that it is properly disposed of.
  2. Don't name things like "sLine". That form of Hungarian is commonly recognized as seriously bad practice. Even Microsoft says don't do it.
  3. You're dealing with columns, not rows. So that variable should be named appropriately.
  4. Always test to make sure you have all of the columns you need before blindly accessing them.
  5. Normally, I wouldn't use Convert.ToDouble or Convert.ToInt32. It's much safer to use TryParse to make sure it was able to convert. The code you have will blow if cols[1] and cols[3] had non-numeric data.
  6. You can use the @ symbol in front of a string to tell the compiler that it doesn't need to be escaped.
  7. It's much cleaner to simply "continue" a loop instead of wrapping it in a if statement.
  8. Setting a String variable to a blank string then immediately setting it to some other value causes the blank to stay in memory for the entire scope. In other words, it's wasting memory. Granted, in this case it's a micro-optimization, but it never hurts to use best practices all of the time.
answered on Stack Overflow Aug 3, 2012 by NotMe • edited Aug 3, 2012 by NotMe
1

Have you considered checking for row.Length before accessing row[1] and row[3]

I suspect your empty lines are the problem

answered on Stack Overflow Aug 3, 2012 by parapura rajkumar
1

Here is how you can do it simpler:

        string[] lines = File.ReadAllLines("d:\\TEST.txt");
        foreach (var line in lines.Where(line => line.Length > 0))
        {
            string[] numbers = line.Split(',');

            // It checks whether numbers.Length is greater than 
            // 3 because if maximum index used is 3 (numbers[3]) 
            // than the array has to contain at least 4 elements
            if (numbers.Length > 3)
            {
                double a = Convert.ToDouble(numbers[1]);
                Console.Write(a);
                int b = Convert.ToInt32(numbers[3]);
                Console.Write(b);
                Console.WriteLine();
            }
        }
answered on Stack Overflow Aug 3, 2012 by Ivan G • edited Aug 3, 2012 by Ivan G
0

You should consider to use :

if (!string.IsNullOrEmpty(sLine))

instead of

if (sLine != null)

You have this exceptions because some lines are empty.

However, here is a way you should write your code when using a StreamReader :

using(var reader = new StreamReader(@"d:\\TEST.txt"))
{
    string line;
    while ((line= reader.ReadLine()) != null)
    {
        if (string.IsNullOrEmpty(line)) continue;

        var rows = line.Split(",".ToCharArray());
        var a = Convert.ToDouble(rows[1]);
        Console.Write(a);
        var b = Convert.ToInt32(rows[3]);
        Console.WriteLine(b);
        Console.WriteLine();
    }
}

Regards,

Kévin

answered on Stack Overflow Aug 3, 2012 by Kévin Rapaille • edited Aug 3, 2012 by Kévin Rapaille

User contributions licensed under CC BY-SA 3.0