2

I have a file contains two lines . and in which line there is a double parameter . I want to read both lines from the file and save them in an array of doubles . I used the C# code below , but It doesn't work . It doesn't read anything and the array is empty after running the code . Anybody has any idea where did I do wrong ? Thanks for help .

    private FileStream input;
    double[] arr;
    int i = 1;

    input = new FileStream(Application.StartupPath+"\\City.txt", FileMode.Open, FileAccess.Read);
    StreamReader reader = new StreamReader(input); 

    while (!reader.EndOfStream)
        {
            arr[i] = Convert.ToDouble(reader.ReadLine());
            i++;
        }

    reader.Close();
Bahareh LV
  • 53
  • 2
  • 8
  • 2
    Please provide a short but *complete* program demonstrating the problem. We don't know whether you've even initialized `arr` anywhere. – Jon Skeet Jul 02 '13 at 19:49
  • did you run the debugger? is the file opened? does the loop process both lines? is your text in a format that can be converted to a double? – Geoff Jul 02 '13 at 19:52
  • I want to show the array parameters in two `textbox` . – Bahareh LV Jul 02 '13 at 19:55
  • the file contains two double parameters like below :>35.5>37.5 – Bahareh LV Jul 02 '13 at 19:56
  • I created the file and saved 2 double parameters in it in another class . and now I want to use that parameters here . I Don't know where did I do wrong ? – Bahareh LV Jul 02 '13 at 19:58

5 Answers5

1

try this approach

using (StreamReader sr = File.OpenText(Application.StartupPath+"\\City.txt")) 
{
    string line;
    // Read and display lines from the file until the end of  
    // the file is reached. 
    while ((line = sr.ReadLine()) != null) 
    {
         arr[i] = Convert.ToDouble(line);
         i++;
    }
}

and you should at least initialize arr: arr = new double[_size] and i should be zero because arrays in c# are zero based. And better use generic collection like List<T>(List<double> in this case).

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
1

This is a complete example of what you are doing.

string line;
List<double> values = new List<double>();
string path = Path.Combine(Application.StartupPath, "City.txt");

System.IO.StreamReader file = new System.IO.StreamReader(path);
while((line = file.ReadLine()) != null)
{
    values.Add(double.Parse(line));
}

file.Close();

Based on "How to: Read a Text File One Line At a Time (MSDN)"

Fenton
  • 241,084
  • 71
  • 387
  • 401
0

The issue is while (!reader.EndOfStream) because when you initially read it in the position is at the end of the file. This is solidified by the fact that the line arr[i] should fail because you've not initialized the array (in fact, it shouldn't even compile...). So, how about this:

double[] arr = new double[2];
...

reader.BaseStream.Position = 0;
while (!reader.EndOfStream)
{
    arr[i] = Convert.ToDouble(reader.ReadLine());
    i++;
}

However, a more straight forward approach would be something like this:

var arr = new List<double>();
var lines = File.ReadAllLines(Application.StartupPath+"\\City.txt");
foreach (var l in lines)
{
    arr.Add(Convert.ToDouble(l));
}
return arr.ToArray();
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

Another option is use File.ReadAllLines, considering that file size is small.

    string[] stringDoubles = File.ReadAllLines(path, Encoding.UTF8);
    for(int i=0;i<stringDoubles.Length;i++)        
        arr[i] = Convert.ToDouble(stringDoubles[i]);
Tigran
  • 61,654
  • 8
  • 86
  • 123
0

The code as you posted will not compile, because you have not initialized your array, as well as having a visibility modifier on your FileStream. I'd guess this code is from two different locations in your project.

However, there's a much simpler way to do this: File.ReadAllLines

string path = @"c:\dev\text.txt"; //your path here
string[] lines = File.ReadAllLines(path);
double[] doubles = new double[2];
for (int i = 0; i < doubles.Length; i++)
{
    double d;
    if (double.TryParse(lines[i], out d))
        doubles[i] = d;
}
Curtis Rutland
  • 776
  • 4
  • 12