正在读取.txt文件并显示文件中的数字
我正在尝试使用c#读取.txt文件并显示其内容,但出现错误代码为IndexOutOfRangeException
,错误代码为0xc000013a
。正在读取.txt文件并显示文件中的数字
这里是我的代码:
static void Main(string[] args)
{
StreamReader sStreamReader = new StreamReader("d:\\TEST.txt");
while (!sStreamReader.EndOfStream)
{
string sLine = "";
if (sLine != null)
{
sLine = sStreamReader.ReadLine();
if (sLine != null)
{
string[] rows = sLine.Split(",".ToCharArray());
double a = Convert.ToDouble(rows[1]);
Console.Write(a);
int b = Convert.ToInt32(rows[3]);
Console.WriteLine(b);
Console.WriteLine();
}
}
}
}
我的文本文件如下:
1,2,3,4,5,6,7
1,2,3,4,5,6,7
5,6,2,7,3,8,4
3,4,3,4,3
5,3,23,12
12,30000,12,99
你有没有考虑访问row[1]
和row[3]
前检查row.Length
我怀疑你的空行是问题
你应该考虑使用:的
if (!string.IsNullOrEmpty(sLine))
代替
if (sLine != null)
因为一些线是空的,您有这样的例外。
但是,这里有一个方法使用一个StreamReader时,你应该写代码:
using(var reader = new StreamReader(@"d:\\TEST.txt"))
{
string line;
while ((line= reader.ReadLine()) != null)
{
if (string.IsNullOrEmpty(line)) continue;
var rows = line.Split(",".ToCharArray());
var a = Convert.ToDouble(rows[1]);
Console.Write(a);
var b = Convert.ToInt32(rows[3]);
Console.WriteLine(b);
Console.WriteLine();
}
}
问候,
凯文
我将它更改为以下:
static void Main(string[] args)
{
// StreamReader is IDisposable which should be wrapped in a using statement
using (StreamReader reader = new StreamReader(@"d:\TEST.txt"))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
// make sure we have something to work with
if (String.IsNullOrEmpty(line)) continue;
string[] cols = line.Split(',');
// make sure we have the minimum number of columns to process
if (cols.Length < 4) continue;
double a = Convert.ToDouble(cols[1]);
Console.Write(a);
int b = Convert.ToInt32(cols[3]);
Console.WriteLine(b);
Console.WriteLine();
}
}
}
这里有一些注意事项:
- StreamReader实现了IDisposable,所以你应该把它包装在一个using子句中,以便正确处理它。
- 不要命名为“sLine”。这种匈牙利形式通常被认为是严重不良的做法。即使微软说不要这样做。
- 你正在处理的是列,而不是行。所以这个变量应该适当地命名。
- 在盲目地访问它们之前,请务必测试以确保您拥有所有需要的列。
- 通常,我不会使用Convert.ToDouble或Convert.ToInt32。使用TryParse确保它能够转换更安全。如果cols [1]和cols [3]具有非数字数据,那么您的代码将会受到攻击。
- 您可以在字符串前面使用@符号来告诉编译器它不需要转义。
- 简单地“继续”一个循环而不是将其包装在if语句中会更简洁。
- 将字符串变量设置为空字符串,然后立即将其设置为某个其他值,导致空白留在整个范围的内存中。换句话说,这是在浪费记忆。诚然,在这种情况下,这是一种微型优化,但它永远不会伤害到使用最佳实践。
这里是你如何能做到这简单:
string[] lines = File.ReadAllLines("d:\\TEST.txt");
foreach (var line in lines.Where(line => line.Length > 0))
{
string[] numbers = line.Split(',');
// It checks whether numbers.Length is greater than
// 3 because if maximum index used is 3 (numbers[3])
// than the array has to contain at least 4 elements
if (numbers.Length > 3)
{
double a = Convert.ToDouble(numbers[1]);
Console.Write(a);
int b = Convert.ToInt32(numbers[3]);
Console.Write(b);
Console.WriteLine();
}
}
+1:我把linq的使用联系起来以找出那些有* something *的行。我建议你在访问数组之前添加一个检查验证数组中是否有正确数量的元素。此外,您可能会对最初的ReadAllLines进行说明,这对于有限的数据量非常有用;但对OP来说可能是完美的。 – NotMe 2012-08-03 13:56:34
@ChrisLively谢谢克里斯,会那样做。 – 2012-08-03 17:20:22
在这行你所得到的例外呢?这可能是因为你正在读取一些不是昏迷分隔的行,因此'split'方法并没有返回你所期望的。 – npinti 2012-08-03 06:00:52
顺便说一句,不应该你的变量“行”实际上被称为“列”或“字段”? – 2012-08-03 06:03:39
检查你的文件是否有空行 也许在最后一行 – Karl 2012-08-03 06:09:27