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();
5
  • 2
    Please provide a short but complete program demonstrating the problem. We don't know whether you've even initialized arr anywhere. Commented Jul 2, 2013 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? Commented Jul 2, 2013 at 19:52
  • I want to show the array parameters in two textbox . Commented Jul 2, 2013 at 19:55
  • the file contains two double parameters like below :>35.5>37.5 Commented Jul 2, 2013 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 ? Commented Jul 2, 2013 at 19:58

5 Answers 5

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)"

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

Comments

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).

Comments

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();

Comments

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]);

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.