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.

5
  • double[] arr = read.ReadFromFile(); Commented Dec 26, 2016 at 12:36
  • 2
    You never put anything in dataString. Commented Dec 26, 2016 at 12:39
  • while (((line = file.ReadLine()) != null) and so on. Look like someone homework.. Commented Dec 26, 2016 at 12:40
  • @Crowcoder i correct it and add (); and use part_of_line[i] instead of empty dataString[i] in data[i] = double.Parse(dataString[i]); but it's still the same Commented Dec 26, 2016 at 13:02
  • 1
    I think, you should set stream to beginning. stackoverflow.com/questions/2053206/… Commented Dec 26, 2016 at 13:18

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.