Streamreader 和 "Index was outside the bounds of the array" 错误
Streamreader and "Index was outside the bounds of the array" error
我没有写入任何数组长度,但是当我尝试读取 .csv 文件时,出现 "Index was outside the bounds of the array" 错误。 .csv 文件大约有 1 000 000 行。有人可以修复此代码吗?
.csv 文件行如下。
0000000,26.0000000000000,38.0000000000000,30.01.2017,0,0,0,,,0,0,,0,,0,0,0,0
string[] read;
char[] seperators = { ',' };
try
{
Image img = Image.FromFile(txtFilePath.Text);
Graphics g = Graphics.FromImage(img);
pictureBox1.Image = img;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
StreamReader sr = new StreamReader(txtFile2Path.Text);
string data;
while ((data=sr.ReadLine()) !=null)
{
read = data.Split(seperators, StringSplitOptions.None);
float x = float.Parse(read[1]);
float y = float.Parse(read[2]);
string z = read[10];
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
只是为了向最终出现在这里的任何人澄清答案..
while ((data=sr.ReadLine()) !=null)
{
read = data.Split(seperators, StringSplitOptions.None);
if (read.Length >= 11)
{
float x = float.Parse(read[1]);
float y = float.Parse(read[2]);
string z = read[10];
}
}
访问可能不是所需长度的数组时,请先进行检查。
仅当一行代码试图访问数组中不存在的项目 (N-1) 时才会抛出 Index was outside the bounds of the array
异常 - 由于数组少于 (N)项目
我没有写入任何数组长度,但是当我尝试读取 .csv 文件时,出现 "Index was outside the bounds of the array" 错误。 .csv 文件大约有 1 000 000 行。有人可以修复此代码吗?
.csv 文件行如下。
0000000,26.0000000000000,38.0000000000000,30.01.2017,0,0,0,,,0,0,,0,,0,0,0,0
string[] read;
char[] seperators = { ',' };
try
{
Image img = Image.FromFile(txtFilePath.Text);
Graphics g = Graphics.FromImage(img);
pictureBox1.Image = img;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
StreamReader sr = new StreamReader(txtFile2Path.Text);
string data;
while ((data=sr.ReadLine()) !=null)
{
read = data.Split(seperators, StringSplitOptions.None);
float x = float.Parse(read[1]);
float y = float.Parse(read[2]);
string z = read[10];
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
只是为了向最终出现在这里的任何人澄清答案..
while ((data=sr.ReadLine()) !=null)
{
read = data.Split(seperators, StringSplitOptions.None);
if (read.Length >= 11)
{
float x = float.Parse(read[1]);
float y = float.Parse(read[2]);
string z = read[10];
}
}
访问可能不是所需长度的数组时,请先进行检查。
仅当一行代码试图访问数组中不存在的项目 (N-1) 时才会抛出 Index was outside the bounds of the array
异常 - 由于数组少于 (N)项目