简体   繁体   中英

How do I read three text files and sort into three row jagged array in C#?

My C# project is a Windows form.

I have three text files containing exam scores for separate class sections in my bin/debug folder.

This is the part I really need help with: I need to store the files in a three row jagged array divided by section .


Lastly, using the jagged array, I need to display each of these calculations in text boxes:

The average score for each individual section

The average score for all sections

The highest score of all sections

The lowest score of all sections.


Section1.txt:

 87
 93
 72
 98
 65
 70
 89
 78
 77
 66
 92
 72

Section2.txt:

 71
 98
 93
 79
 84
 90
 88
 91

Section3.txt:

 88
 81
 56
 72
 69
 74
 80
 66
 71
 73

Firstly,You have three text file stored in bid/debug folder.

Section1.txt, Section2.txt, Section3.txt

You can get this text files path.right? So you can store this text file path to string array.

Second:You can get data from each text file by looping this string array.Here how to read data from text file => http://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line .

You can get Section 1 string array by splitting string data with "Space" or "\\n".

Let's start from reading one file into array; it's easy with a help of Linq :

using System.IO;
using System.Linq;

...

string path = @"C:\MyFile1.txt";

int[] result = File
  .ReadLines(path)
  .Select(line => int.Parse(line))
  .ToArray(); 

Now, let's have not a single file, but a collection of them:

string[] filePaths = new string[] {
  @"C:\MyFile1.txt",
  @"C:\MyFile2.txt",
  @"C:\MyFile3.txt",
};

int[][] result = filePaths
  .Select(path => File //The inner code looks familiar, right?
    .ReadLines(path)
    .Select(line => int.Parse(line))
    .ToArray()) 
  .ToArray();

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