为什么这个 StreamReader 只读取一行?
下面的代码应该读取文件的每一行并对其进行操作。但是,它只读取第一行。如果没有 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
The following piece of code should read each line of the file and operate on it. However, it only reads the first line. Without the for loop it reads the whole file. I honestly have no idea why it's not reading the whole thing.
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++;
}
Without the for loop the following works and prints the whole file:
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 is as follows:
private bool CheckInput(char c)
{
switch (c)
{
case 'y':
return true;
case 'n':
return false;
default:
return true;
}
}
A sample input file:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你在 for 循环中遇到异常吗?您正在递增 i,也许在某些时候您试图错误地索引
lineCh
。编辑:索引错误的另一个候选者是
grid
数组。我没有看到初始化代码,x
和y
的值是在读取文件后确定的。你如何初始化它?Are you getting an exception in the for loop? You are incrementing i, maybe at some point you are trying to index incorrectly
lineCh
.EDIT: another candidate for bad indexing is the
grid
array. I see no initialization code, and the values ofx
andy
are determined after reading the file. How do you initialize it?您正在修改循环体内的循环控制变量,这是您应该避免的事情,因为它会导致循环的意外执行。
请显示您正在尝试处理的行的示例,我也许可以建议更好地实现您的 for 循环。
您是否需要立即处理整行,还是需要将其分成 4 个字符的块,处理这 4 个字符,然后移至下一行?
您可以尝试更改处理该行的方式:
这里我根据空格拆分该行,然后我可以通过拆分为字符数组并访问特定字符来对各个段进行操作。
这段代码还假设每个段总是有 4 个字符,情况总是如此吗?您还应该添加验证以确保该行符合您的预期。
You are modifying a loop control variable inside the body of your loop, this is something you should avoid as it lead to unexpected execution of your loop.
Please show a sample of the line that you are trying to process and I maybe able to suggest a better implementation of your for loop.
Do you need to process the entire line at once or do you need to break it into chunks of 4 characters, process these 4, then move onto the next line?
You could try changing how you process the line:
Here I split the line based on the spaces, then I can operate on the individual segment by splitting in into a character array and accessing the specific characters.
This code also makes the assumption that there will always be 4 characters to each segment, will this always be the case? You should also add validation to ensure that the line is what you expect.
您的代码会引发异常,因为您可以在任何这些行处超出数组范围:
your code throws exception because you can get out array bounds at any of those lines:
我认为你的问题可能是......
结合许多++i语句。
这是带有大量注释的代码...假设每一行都是“1234”。
I think your problem may be...
Combined with the many ++i statements.
Here's the code with a load of comments... assumes each line is "1234".
在循环本身内增加循环变量是危险的。我建议为您的北、东等变量创建一个自定义类型,然后消耗每一行到最后。或者更好的是返回下一个 GridSquare 对象。
这可以通过返回 GridSquares 迭代器的方法来完成:
GetGridSquares 是:
It is dangerous to increment the looping variable inside the loop itself. I would recommend to create a custom type for your north, east, etc. variables and then consume each line to the end. Or maybe even better return the next GridSquare object.
This could be done with a method returning an iterator for GridSquares:
GetGridSquares is:
我在清理代码后发现的实际答案是 x 从未被设置回零;我们永远不会移动到网格的下一行[,]。我意识到从我的例子中很难解决这个问题,对此我深表歉意。
The actual answer I found after cleaning the code was that x is not being set back to zero ever; we never move to the next row of the grid[,]. I realise this was hard to work out from my examples, my apologies there.