gpt4 book ai didi

Why Is ReadLine() Skipping certain lines(为什么ReadLine()跳过某些行)

转载 作者:bug小助手 更新时间:2023-10-24 19:06:20 25 4
gpt4 key购买 nike



Overarching plan, make a cyoa game. Things have been working great for a while. But then errors I can't understand have been occuring. Debug.Log() print statements not working or skipping for some reason, but data still be used properly at times.I decided to scrap and start again just to make sure I'm reading my graph data correctly. Below I'll attach the data I'm trying to read, the code I'm using to read it, and what the Debug console is outputting. I'm running this in Unity with C#. I'm sorry if this is stupid or has been answered elsewhere, I'm not fully getting what's wrong. I suspect it might be invisible endline characters, but then I don't even know how to fix that.

总体规划,做一个青年团游戏。一段时间以来,情况一直很好。但是,我不能理解的错误一直在发生。Debug.Log()打印语句由于某种原因无法工作或跳过,但数据有时仍可正确使用。我决定放弃并重新开始,以确保正确读取图形数据。下面我将附加我试图读取的数据、我用来读取它的代码以及调试控制台正在输出的内容。我正在用C#在Unity中运行它。很抱歉,如果这是愚蠢的,或者已经在其他地方得到了回答,我没有完全明白哪里出了问题。我怀疑它可能是不可见的结尾字符,但我甚至不知道如何修复它。


The Graph Data I'm reading in

我正在读入的图形数据


0 id    
{The following questions will help determine ...
Personal Power
> What you know
> Who you know
> What you have
> Who you are
> ( skip ) I know my House Arcana...
No Info
1 id
1 id
Mind + 2 Strength - 1 Sneakiness - 1 Heart - 1 endline
No Info
id 1 id
Mind - 1 Strength - 1 Sneakiness - 1 Heart + 2 endline
No Info
1 id
Mind - 1 Strength - 1 Sneakiness + 2 Heart - 1 endline
No Info
1 id
Mind - 1 Strength + 2 Sneakiness - 1 Heart - 1 endline
No Info
3 id
No Info
No Info

The above is all the data of a Node in the structure I made, not even getting sent or used anywhere at this point.

以上是我创建的结构中某个节点的所有数据,甚至在这一点上都没有发送或使用。


The code below is what I'm using to read and parse the information. The filepath is correct and that all works. It gets the majority of the data, just not all of it

下面的代码是我用来读取和解析信息的。文件路径是正确的,一切正常。它获取了大部分数据,只是不是全部


string line;
//c1s.text = "it gets to this at all"; - this worked so it does get here.
string filepath = "Assets/Resources/GraphData.txt";
try
{
using (StreamReader sr = new StreamReader(filepath))
{
//int i = 0;
while ((line = sr.ReadLine()) != "endgame")
{

Debug.Log(line);
}
sr.Close();
}

}
catch (Exception e)
{
Console.WriteLine("The File could not be read:");
Console.WriteLine(e.Message);
}

Finally here is the output from the debug.log

最后,以下是调试文件.log的输出


0 id    
{The following questions will help determine ...
Personal Power
> What you know
> Who you know
> What you have
> Who you are
> ( skip ) I know my House Arcana...
No Info
1 id
1 id
Mind + 2 Strength - 1 Sneakiness - 1 Heart - 1 endline
Mind - 1 Strength - 1 Sneakiness - 1 Heart + 2 endline
Mind - 1 Strength - 1 Sneakiness + 2 Heart - 1 endline
Mind - 1 Strength + 2 Sneakiness - 1 Heart - 1 endline
3 id

I've been changing the format, adding and removing spaces at the ends of lines, deleting and rewriting lines, the output always changes somewhat but it's never consistent, or even close to all of the above. I can't for the life of me get what's causing some lines to be read and some skipped. Even adding "endline" to the end of some of those lines has been inconsistent. Sometimes I can see one or two lines that aren't there, but it varies, and they aren't even the ones I edit necessarily.

我一直在更改格式,在行尾添加和删除空格,删除和重写行,输出总是会有一些变化,但从来不是一致的,甚至是接近上面所有的。我无论如何也搞不懂是什么导致了一些行被读,一些行被跳过。甚至在其中一些行的末尾加上“Endline”也是不一致的。有时我可以看到一两行不在那里,但它是不同的,它们甚至不是我必须编辑的。


更多回答

and the count numbers at the right dont go up?

右边的计数数字不会上升吗?

The only problem I see with your StreamReader code is that you need to check to see whether you reached the end of the stream by checking && line != null. Absent that, if endgame is missing from the file, you have an infinite loop. See dotnetfiddle.net/rNDV6W.

我在StreamReader代码中看到的唯一问题是,您需要通过检查&&line!=NULL来检查是否到达了流的末尾。否则,如果文件中缺少endGame,您就会有一个无限循环。参见dotnetfiddle.net/rNDV6W。

I'm not a Unity developer, but could it be that Debug.Log() isn't working as expected? See Print/Debug.Log is not showing output on Unity Console or Console not outputting from Debug.Log() or Why do some cases of Unity's Debug.Log() not work? or Debug.Log()/Print()/UnityEngine.Debug.Log() not working for cases I found where this happened in the past.

我不是Unity开发人员,但会不会是Debug.Log()没有按预期工作?参见打印/调试。日志没有显示Unity控制台或控制台上的输出不是从Debug.Log()输出,或者为什么某些Unity的Debug.Log()不起作用?或者Debug.Log()/print()/UnityEngine.Debug.Log()在我发现过去发生这种情况的情况下不起作用。

Rather than depending on visual inspection tools like Debug.Log() to verify the correctness of your code, consider using unit testing to verify correctness, e.g. like so: dotnetfiddle.net/Q7vUc4

与其依赖像Debug.Log()这样的视觉检查工具来验证代码的正确性,不如考虑使用单元测试来验证正确性,例如:dotnetfiddle.net/Q7vUc4

Reason for my question is that is a set of the unique output. I bet at the end the lines that have been repeated get little numbers. Because you have the console set to do so

我提出这个问题的原因是,这是一套独特的输出。我敢打赌,在最后,重复的台词得到的数字很少。因为您已将控制台设置为这样做

优秀答案推荐

I tested your code on my local and it works fine, I suspect that your debugger doesn't really log the line properly due to some configurations on your end. Try use Console.WriteLine and see the result directly from the console.

我在我的本地测试了您的代码,它工作得很好,我怀疑您的调试器并没有真正正确地记录行,因为您的端有一些配置。尝试使用Console.WriteLine并直接从控制台查看结果。


string line;
string filepath = "./GraphData.txt";
try
{
Console.WriteLine("start");
using (StreamReader sr = new StreamReader(filepath))
{
//int i = 0;
while ((line = sr.ReadLine()) != "endgame")
{
Console.WriteLine(line);
}

Console.WriteLine("end");
Console.ReadLine();
sr.Close();
}
}
catch (Exception e)
{
Console.WriteLine("The File could not be read:");
Console.WriteLine(e.Message);
}


enter image description here


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com