How to read from a file using C# code?

旧街凉风 提交于 2019-12-11 03:09:51

问题


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

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




回答2:


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




回答3:


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



回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/17434519/how-to-read-from-a-file-using-c-sharp-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!