c#初学者使用stream reader读入一个txt文件
c# beginner using stream reader to read in a txt file
我在读取一个文本文件时遇到问题,该文件包含 9 组由逗号分隔的三个整数值。这是我到目前为止所做的,但是我如何才能读取第一行的数据以获得最大值?
非常坚持一个程序,数据文本文件看起来像
- 21,7,11
- 20,10,12
- 17,7,18
这些代表温度、高度和碳%
我已经阅读了文件
{
string s;
System.IO.StreamReader inputFile = new System.IO.StreamReader(DataFile);
s = inputFile.ReadLine();
int noDataLines = int.Parse(s);
double[,] data = new double[noDataLines, 3];
string[] ss;
is this right if the data is stored in the debug folder as a .txt file?
从这里我将如何获得最大温度(即只读取数据的第一个垂直列)?
我们可以简单地混合使用 System.IO File.ReadLines()
方法和 LINQ .ToList()
来读取所有文本行到 List<string>
。在这一点上,我们可以遍历集合,从文本行中解析双精度值:
List<string> lines = File.ReadLines("filepath").ToList();
List<int[]> values = new List<int[]>();
int[] temp = new int[3];
for (int i = 0; i < lines.Count; i++)
{
string[] strValues = lines[i].Split(',');
for (int i2 = 0; i2 < strValues.Length; i2++)
temp[i2] = Convert.ToInt32(strValues[i2]);
values.Add(temp.ToArray());
}
或者我们可以使用 LINQ :
List<string> lines = File.ReadLines("filepath").ToList();
List<int[]> values = new List<int[]>();
int[] temp = new int[3];
for (int i = 0; i < lines.Count; i++)
values.Add(lines[i].Split(',')
.Select(l => Convert.ToInt32(l)).ToArray());
我在读取一个文本文件时遇到问题,该文件包含 9 组由逗号分隔的三个整数值。这是我到目前为止所做的,但是我如何才能读取第一行的数据以获得最大值? 非常坚持一个程序,数据文本文件看起来像
- 21,7,11
- 20,10,12
- 17,7,18
这些代表温度、高度和碳% 我已经阅读了文件
{
string s;
System.IO.StreamReader inputFile = new System.IO.StreamReader(DataFile);
s = inputFile.ReadLine();
int noDataLines = int.Parse(s);
double[,] data = new double[noDataLines, 3];
string[] ss;
is this right if the data is stored in the debug folder as a .txt file?
从这里我将如何获得最大温度(即只读取数据的第一个垂直列)?
我们可以简单地混合使用 System.IO File.ReadLines()
方法和 LINQ .ToList()
来读取所有文本行到 List<string>
。在这一点上,我们可以遍历集合,从文本行中解析双精度值:
List<string> lines = File.ReadLines("filepath").ToList();
List<int[]> values = new List<int[]>();
int[] temp = new int[3];
for (int i = 0; i < lines.Count; i++)
{
string[] strValues = lines[i].Split(',');
for (int i2 = 0; i2 < strValues.Length; i2++)
temp[i2] = Convert.ToInt32(strValues[i2]);
values.Add(temp.ToArray());
}
或者我们可以使用 LINQ :
List<string> lines = File.ReadLines("filepath").ToList();
List<int[]> values = new List<int[]>();
int[] temp = new int[3];
for (int i = 0; i < lines.Count; i++)
values.Add(lines[i].Split(',')
.Select(l => Convert.ToInt32(l)).ToArray());