下面的代码应该读取文件的每一行并对其进行操作。但是,它只读取第一行。没有for循环,它会读取整个文件。老实说,我不知道为什么不读整篇文章。
StreamReader sr = new StreamReader(gridPath);
string line;
char[] lineCh;
char current;
int x, y;
bool north, east, south, west;
x = y = 0;
while ((line = sr.ReadLine()) != null)
{
lineCh = line.ToCharArray();
for (int i = 0; i < lineCh.Length; i++)
{
current = lineCh[i];
north = CheckInput(current);
current = lineCh[++i];
east = CheckInput(current);
current = lineCh[++i];
south = CheckInput(current);
current = lineCh[++i];
west = CheckInput(current);
i++; // Hop over space
grid[x, y] = new GridSquare(north, east, south, west);
x++; // Start next column
}
Console.WriteLine(line);
y++;
}
如果没有for循环,则以下工作并打印整个文件:
StreamReader sr = new StreamReader(gridPath);
string line;
char[] lineCh;
char current;
int x, y;
bool north, east, south, west;
x = y = 0;
while ((line = sr.ReadLine()) != null)
{
lineCh = line.ToCharArray();
Console.WriteLine(line);
y++;
}
sr.Close();
CheckInput如下:
private bool CheckInput(char c)
{
switch (c)
{
case 'y':
return true;
case 'n':
return false;
default:
return true;
}
}
示例输入文件:
nyyn nyyy nyyy nyyy nyyy nnyy
yyyn yyyy yyyy yyyy yyyy ynny
yyyn yyyy yyyy yyyy ynyy nnnn
yyyn yyyy yyyy yyyy ynyy nnnn
yyyn yyyy yyyy yyyy yyyy nnyy
yynn yyny yyny yyny yyny ynny
答案 0 :(得分:6)
你是否在for循环中遇到异常?你正在递增i,也许在某些时候你试图错误地索引lineCh
。
编辑:错误索引的另一个候选者是grid
数组。我没有看到初始化代码,并且在读取文件后确定x
和y
的值。你如何初始化它?
答案 1 :(得分:3)
您正在修改循环体内的循环控制变量,这是您应该避免的,因为它会导致意外执行循环。
请显示您尝试处理的行的示例,我或许可以建议您更好地实现for循环。
您是否需要立即处理整行,还是需要将其分成4个字符的块,处理这4个字符,然后移到下一行?
您可以尝试更改处理线的方式:
while ((line = sr.ReadLine()) != null)
{
string[] segments = line.Split(' ');
foreach(string segment in segments)
{
char[] arr = segment.ToCharArray();
north = CheckInput(arr[0]);
east = CheckInput(arr[1]);
west = CheckInput(arr[2]);
south = CheckInput(arr[3]);
grid[x, y] = new GridSquare(north, east, south, west);
}
Console.WriteLine(line);
y++;
}
这里我根据空格分割线条,然后我可以通过拆分成一个字符数组并访问特定字符来操作单个段。
此代码还假设每个段总有4个字符,这总是如此吗?您还应该添加验证以确保该行符合您的预期。
答案 2 :(得分:1)
我认为你的问题可能是......
for (int i = 0; i < lineCh.Length; i++)
结合许多++ i语句。
这是带有大量评论的代码......假设每一行都是“1234”。
StreamReader sr = new StreamReader(gridPath);
string line;
char[] lineCh;
char current;
int x, y;
bool north, east, south, west;
x = y = 0;
while ((line = sr.ReadLine()) != null)
// line is "yyyy"
{
lineCh = line.ToCharArray();
// lineCh.Length is 4
for (int i = 0; i < lineCh.Length; i++)
{
current = lineCh[i]; // i is zero
north = CheckInput(current);
current = lineCh[++i]; // i is 1
east = CheckInput(current);
current = lineCh[++i]; // i is 2
south = CheckInput(current);
current = lineCh[++i]; // i is 3
west = CheckInput(current);
i++; // Hop over space // i is 4
grid[x, y] = new GridSquare(north, east, south, west);
// (true,true,true,true)
// So essentially the loop ends if there are four,
// or goes round again for multiples of 4 - of course,
// it will error if there is ever 3, or 5 or any other non multiple of 4
x++; // Start next column
}
答案 3 :(得分:1)
您的代码抛出异常,因为您可以在以下任何一行中获取数组边界:
current = lineCh[++i];
答案 4 :(得分:1)
在循环内部递增循环变量是危险的。我建议为您的north,east等变量创建一个自定义类型,然后将每一行消耗到最后。或者甚至可以更好地返回下一个GridSquare对象。
这可以通过返回GridSquares迭代器的方法来完成:
StreamReader sr = new StreamReader("input.txt");
string line;
char[] lineCh;
char current;
int x, y;
bool north, east, south, west;
x = y = 0;
while ((line = sr.ReadLine()) != null)
{
foreach (var gs in GetGridSquares(line))
{
// grid[x, y] = gs;
}
Console.WriteLine(line);
y++;
}
GetGridSquares是:
private IEnumerable<GridSquare> GetGridSquares(string line)
{
var splittedLine = line.Split(' ');
foreach (var gsStr in splittedLine)
{
if (gsStr.Length != 4)
{
continue;
}
yield return new GridSquare(gsStr[0], gsStr[1], gsStr[2], gsStr[3]);
}
}
答案 5 :(得分:1)
StreamReader sr = new StreamReader(gridPath);
var line;
var y = 0;
while ((line = sr.ReadLine()) != null)
{
for(var i =0; i<line.length;i+=2)
{
grid[i,y]=new GridSquare(GetBits(line[i],i));
grid[i+1,y]=new GridSquare(GetBits(line[i],i+1));
}
++y;
}
bool [] GetBits(char bBytes, int n)
{
var returned = new bool[4];
bBytes = bBytes << ((n%2)*4);
for(var i =0; i < 4; ++i)
returned[i]=(bBytes & (1<<i ) > 0;
}
答案 6 :(得分:1)
清理代码后我发现的实际答案是x没有被设置回零;我们永远不会移动到网格的下一行[,]。我意识到这很难从我的例子中解决,我在那里道歉。