我已经做了好几个小时了--我已经是我工作的最后一部分了。我需要弄清楚如何将循环限制为从文本行上定义的rang中获取值。就像第1-5行,第6-10行,等等。
TextReader tr = new StreamReader("values.txt");
for (int i = 0; i <= 5; i++)
{
for (int a = 0; a <= 5; a++)
{
line = tr.ReadLine();
**while ((line = tr.ReadLine()) != null)**
for (a = 0; a <= 5; a++){
ln = tr.ReadLine();
if (ln != null){
value = int.Parse(ln);
if (value > max)
max = value;
if (value < min)
min = value;}
}
Console.WriteLine(tr.ReadLine());
if (i % 5 == 0)
Console.WriteLine("The max is" + max);
if (i % 5 == 0)
Console.WriteLine("The min is" + min);
if (i % 5 == 0)
Console.WriteLine("-----First 5");
}我已经准备好上床睡觉了,但我不想睡觉失败了。任何以正确的方式推动将不胜感激。
发布于 2011-10-10 09:53:09
如果您想使用StreamReader (例如,因为预期文件大小很大),您可以这样做:
using (TextReader tr = new StreamReader("values.txt"))
{
string currentLine = null;
do
{
Console.WriteLine("processing set of 5 lines");
for (int i = 0; i < 5; i++)
{
currentLine = tr.ReadLine();
if (currentLine == null)
{
break;
}
Console.WriteLine("processing line {0} of 5", i);
// your code goes here:
}
} while (currentLine != null);
}编辑:和您应该在using block (代码更新)中使用StreamReader。
https://stackoverflow.com/questions/7710768
复制相似问题