简体   繁体   中英

Reading .txt file into an array

I need to be able to read a text file into an array instead of inputting all the values myself. The text file reads as:

8.7
9.3
7.9
6.4
9.6
8.0
8.8
9.1
7.7
9.9
5.8
6.9

The main purpose of the program is to read scores from a data file, store them in an array, and calculate the highest, lowest, total and average of the 12 scores.

The text file is stored in the Debug folder of the project.

This is what I've done so far:

        Console.WriteLine("Numbers in the list: " + scores.Length);


        //highest number
        double high = scores[0];

        for (int index = 1; index < scores.Length; index++)
        {
            if (scores[index] > high)
            {
                high = scores[index];
            }
        }

        Console.WriteLine("Highest number = " + high);

        //lowest number
        double low = scores[0];

        for (int index = 1; index < scores.Length; index++)
        {
            if (scores[index] < low)
            {
                low = scores[index];
            }
        }

        Console.WriteLine("lowest number = " + low);

        //average of the scores
        double total = 0;
        double average = 0;

        for (int index = 0; index < scores.Length; index++)
        {
            total = total + scores[index];
        }

        average = (double)total / scores.Length;

        Console.WriteLine("Total = " + total);
        Console.WriteLine("Average = " + average.ToString("N2"));
        Console.ReadKey();



    }
}

}

You have no code in place for reading a file, or writing a file so that will be your problem. I suggest you start with looking at: StreamReader Class (MSDN) .

Then you will probably want to look at the following: String.Split Method (MSDN)

To make the program a bit more dynamic you possible want to consider looking at this:

Application.ExecutablePath Property (MSDN)

Enviroment.GetFolderPath Method (MSDN) (This allows you to store data in better locations)

^^^^Original Response above^^^^^

Another option you could consider is described in a lot of detail here:

Working with Files - Code Project Article

You can use File.ReadLines method to read the lines lazily (means doesn't load all the lines to memory at once) from your file and Select method to take each line and parse it to double :

var values = File.ReadLines("path")
            .Select(line => double.Parse(line))
            .ToArray();

Then you can use Max , Min , Average methods from LINQ to get highest,lowest number and the average.

All of this can be writen easier :

using System;
using System.IO;
using System.Linq;

String path = @"MyFolder\myfile.txt"; // path for your file

// read the file
String[] lines = File.ReadAllLines(path);
// convert data in Doubles    
Double[] data = Array.ConvertAll(lines, Double.Parse);

// use Linq to get what you want (min, max, total, average, ...)
Double low = data.Min();
Double high = data.Max();
Double total = data.Sum();
Double average = data.Average();

var scores = File.ReadAllLines(“ scores.txt”)。Select(x => Double.Parse(x))。ToArray();

var numbers = File.ReadAllLines(@"C:\numbers.txt")
                  .Select(n => double.Parse(n));
var max = numbers.Max();
var min = numbers.Min();
var sum = numbers.Sum();
var avg = numbers.Average();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM