How to load a number list into a TXT file for a C#list?

Asked

Viewed 180 times

1

I have the following list of numbers loaded from a table, and 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.

  • https://answall.com/questions/92567/ler-todo-conte%C3%Bado-de-a-text-file

  • What type of vector??? why can each line be placed on a vector or each line representing each position?

2 answers

1

Using

 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 divide the line by |
  • .ToList(); will send everything to the list

0

If you want to play the read data from the file in an array or 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.