Reading data from a CSV to an array of arrays (C#)

前端 未结 2 512
攒了一身酷
攒了一身酷 2021-01-28 03:13

I have the CSV file opened, but I can\'t figure out how to put the resultant array from splitting the line into another array. I have the following code currently, hopefully it

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-28 03:40

    You need to initialize your array, to do that you need to know how many lines in there.

    Instead of reading line by line you can do:

    string[][] FP_GamesArray = File.ReadLines("path")
                              .Select(line => line.Split(','))
                              .ToArray();
    

    Or altenatively, you can start with a List, use it's add method, then convert it to an array after the reading is finished, like below:

    List lines = new List();
    while (!file.EndOfStream)
    {
         string line = file.ReadLine();
         if (!String.IsNullOrWhiteSpace(line))
         {
             lines.Add(line.Split(',');   
         }
    }
    
    string[][] FP_GamesArray = lines.ToArray();
    

提交回复
热议问题