1

I am writing a class that contains a method that should return an array after reading from file, but it always return an empty array. When I call the method without class just in main file everything works fine.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectX
{
    class ReadFile
    {

        private System.IO.StreamReader file;

        public ReadFile()
        {         
            file = new System.IO.StreamReader(@"E:\\XXXX.csv"); 

        }

        private int DataLenght()
        {

            String line = String.Empty;         
            int lenght = 0;
            line = string.Empty;

            while ((line = file.ReadLine()) != null)
            {
                lenght++;

            }
            return lenght;

        }

          public double[] ReadFromFile()
        {
            double[] arr;
            String line = String.Empty;
            double[] data = new double[DataLenght()];
            string[] dataString = new string[DataLenght()];
            List<double> listaDouble = new List<double>();


            int x = 0;
            String[] parts_of_line;
            while ((line = file.ReadLine()) != null)
            {

                parts_of_line = line.Split(',');

                for (int i = 0; i < parts_of_line.Length; i++)
                {
                    parts_of_line[i] = parts_of_line[i].Trim();
                    data[i] = double.Parse(dataString[i]);              
                    listaDouble.Add(data[i]);

                }

            }
            arr = listaDouble.ToArray();

            return arr;
        }

    }
}

and then in main I want to:

ReadFile read = new ReadFile();
double[] arr = read.ReadFromFile;

Sorry for few not needed conversion in ReadFromFile method but try few things to make it works.

Bizhan
  • 16,157
  • 9
  • 63
  • 101
user1974549
  • 103
  • 1
  • 6

1 Answers1

1

You have a number of problems. As your question is at the time of this answer your problem is that you have read the stream and it is now at the end as Andrei Rudik points out. You would have to set it back to the beginning before you try to read it each time:

file.BaseStream.Position = 0;

However, you have more issues after that. Your entire class can be refactored to this, read it carefully and understand what is happening:

class ReadFile
{
    public double[] ReadFromFile()
    {
        List<double> listaDouble = new List<double>();

        using(var file = new System.IO.StreamReader(@"E:\XXXX.csv"))
        {
            string line = "";
            while ((line = file.ReadLine()) != null)
            {
                string[] linetokens = line.Split(',');

                listaDouble.AddRange(linetokens.Select (l => double.Parse(l)));
            }
        }
        return listaDouble.ToArray();
    }
}

Note, this assumes your line tokens always parse to double which is an error prone assumption.

Crowcoder
  • 11,250
  • 3
  • 36
  • 45