How to load a list of numbers in a TXT file to a list in C #?

1

I have the following list of numbers loaded from a table, and it is separated by the character "|":

40 |16000|20|311258|3
40 |17000|20|857392|5
50 |18000|90|528777|1
60 |19000|20|917336|3
60 |20000|20|850348|3
60 |21000|90|72598 |4
100|36000|20|157884|2
100|37000|20|550902|1
10 |1000 |50|932557|1
10 |2000 |50|410520|1

I want to pass this data to a vector from that TXT file.

    
asked by anonymous 28.02.2018 / 13:22

2 answers

1

Using linq

 List<string[]> lista = File.ReadLines("NomeDoArquivo.txt")
                          .Select(line => line.Split('|'))
                          .ToList();
  • File.ReadLines("NomeDoArquivo.txt") to read the file line by line
  • .Select(line => line.Split('|')) will split the line by |
  • .ToList(); will send everything to list
28.02.2018 / 14:38
0

If you want to play the data read from the file in an array or in a list, the following code will serve:

string[] lines = File.ReadAllLines(fileName);

List<string> list = new List<string>();
list.AddRange(lines);

// ou

string[] vect = new string[lines.length];

for(int i = 0; i < lines.length; i++)
{
    vect[i] = lines[i];
}

I believe it will work

    
28.02.2018 / 14:24