求助C#读取txt内容多行如何存到数组里

如题所述

为了处理方便,将数据文件修改一下,增加一个列数 5(每行数据的个数)
7, 5
1, 1, 1, 1 , 1
1, 1, 1, 1 , 1
1, 1, 1, 1 , 1
1, 1, 1, 1 , 1
1, 1, 1, 1 , 1
1, 1, 1, 1 , 1
1, 1, 1, 1 , 1

假设数据文件名为data.tx

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

StreamReader rd = File.OpenText("data.txt");
string s = rd.ReadLine();
string[] ss = s.Split(',');

int row = int.Parse(ss[0]); //行数
int col = int.Parse(ss[1]); //每行数据的个数

double[,] p1 = new double[row,col]; //数组

for (int i = 0; i < row; i++) //读入数据并赋予数组
{
string line = rd.ReadLine();
string[] data = line.Split(',');
for (int j = 0; j < col; j++)
{
p1[i, j] = double.Parse(data[j]);
}
}
}
}
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答