在 C# 中读取文本文件以进行行到行操作的有效方法
Effective way of reading text file in c# for line to line operation
我在读取文本文件以进行行到行添加操作时遇到问题。我使用了以下语法
StreamReader reader = new StreamReader("input.txt");
string line;
while ((line = reader.ReadLine()) != null)
string[] splitted = line.Split('#');
string first = splitted[0].Trim();
string second = splitted[1].Trim();
如果文件具有以下值,我已使用此语法将输入与文本文件分开。
12#15
15#7
13#14
23#31
x= Convert.ToInt32(first);
y= Convert.ToInt32(second);
sum = x+y;
txtBox.text = Convert.ToString(sum);
问题是它只执行最后一行。它只计算 23 和 31 的总和,只显示,但我想先添加 12 和 15,然后在文本框中显示,同样我想添加其他。请帮助我形成适当的语法。
问题是模糊一个,不过,我建议使用Linq:
var source = File
.ReadLines("input.txt") // read line by line
.Select(line => line.Split('#')) // split each line
//.Where(items => items.Length == 2) // you may want to filter out the lines
.Select(items => new { // convert each line into anonymous class
first = items[0].Trim(),
second = items[1].Trim()
});
您可以根据需要添加任意数量的 Select
(行到行操作)。然后你可以在 foreach
循环中处理这些项目:
foreach (var item in source) {
...
// Let's read some fields from the anonymous object
var first = item.first;
var second = item.second;
...
}
编辑: 根据 编辑的 问题,您只想总结一下,可以通过 Linq 还有:
var result = File
.ReadLines("input.txt")
.Select(line => line.Split('#'))
//.Where(items => items.Length == 2) // you may want to filter out the lines
.Sum(items => int.Parse(items[0]) + int.Parse(items[1]));
txtBox.text = result.ToString();
它不仅读取最后一行,而且您永远不会对其他迭代进行任何操作。目前你只是不断重新分配 line
与已阅读的最新行的值,我想你希望将这些保存到列表或类似的
StreamReader reader = new StreamReader("input.txt");
string line;
List<string> allLines = new List<string>();
while ((line = reader.ReadLine()) != null)
allLines.Add(line);
在这里您可以测试您的文件并将数据加载到数据表中,这应该非常简单。
DataTable dtTextFileData = new DataTable();
dtTextFileData.Columns.AddRange(new []
{
new DataColumn("First", typeof(string)),
new DataColumn("Second", typeof(string))
});
StreamReader file = new StreamReader(@"c:\YourFilePath\input.txt");
string line = file.ReadLine();
while (line != null)
{
string[] fields = line.Split('#');
DataRow dr = dtTextFileData.NewRow();
dr["First"] = fields[0].ToString();
dr["Second"] = fields[1].ToString();
dtTextFileData.Rows.Add(dr);
line = file.ReadLine();
}
我在读取文本文件以进行行到行添加操作时遇到问题。我使用了以下语法
StreamReader reader = new StreamReader("input.txt");
string line;
while ((line = reader.ReadLine()) != null)
string[] splitted = line.Split('#');
string first = splitted[0].Trim();
string second = splitted[1].Trim();
如果文件具有以下值,我已使用此语法将输入与文本文件分开。
12#15
15#7
13#14
23#31
x= Convert.ToInt32(first);
y= Convert.ToInt32(second);
sum = x+y;
txtBox.text = Convert.ToString(sum);
问题是它只执行最后一行。它只计算 23 和 31 的总和,只显示,但我想先添加 12 和 15,然后在文本框中显示,同样我想添加其他。请帮助我形成适当的语法。
问题是模糊一个,不过,我建议使用Linq:
var source = File
.ReadLines("input.txt") // read line by line
.Select(line => line.Split('#')) // split each line
//.Where(items => items.Length == 2) // you may want to filter out the lines
.Select(items => new { // convert each line into anonymous class
first = items[0].Trim(),
second = items[1].Trim()
});
您可以根据需要添加任意数量的 Select
(行到行操作)。然后你可以在 foreach
循环中处理这些项目:
foreach (var item in source) {
...
// Let's read some fields from the anonymous object
var first = item.first;
var second = item.second;
...
}
编辑: 根据 编辑的 问题,您只想总结一下,可以通过 Linq 还有:
var result = File
.ReadLines("input.txt")
.Select(line => line.Split('#'))
//.Where(items => items.Length == 2) // you may want to filter out the lines
.Sum(items => int.Parse(items[0]) + int.Parse(items[1]));
txtBox.text = result.ToString();
它不仅读取最后一行,而且您永远不会对其他迭代进行任何操作。目前你只是不断重新分配 line
与已阅读的最新行的值,我想你希望将这些保存到列表或类似的
StreamReader reader = new StreamReader("input.txt");
string line;
List<string> allLines = new List<string>();
while ((line = reader.ReadLine()) != null)
allLines.Add(line);
在这里您可以测试您的文件并将数据加载到数据表中,这应该非常简单。
DataTable dtTextFileData = new DataTable();
dtTextFileData.Columns.AddRange(new []
{
new DataColumn("First", typeof(string)),
new DataColumn("Second", typeof(string))
});
StreamReader file = new StreamReader(@"c:\YourFilePath\input.txt");
string line = file.ReadLine();
while (line != null)
{
string[] fields = line.Split('#');
DataRow dr = dtTextFileData.NewRow();
dr["First"] = fields[0].ToString();
dr["Second"] = fields[1].ToString();
dtTextFileData.Rows.Add(dr);
line = file.ReadLine();
}