【发布时间】:2018-09-23 23:04:32
【问题描述】:
我最近开始使用 C# 进行编码,但我的基于文本的游戏遇到了一个奇怪但令人沮丧的问题。
文本文件不会将我写在文本文件中的“\n”文字打印为换行符。我不能放置实际的行,因为 ReadAllLines() 不会再正确拆分描述了。
这里有一个例子来表达我的意思
在 roomdata.txt 中
You see an inn to the east, the everywhere else is covered in nothing but trees.\nYoucan make out a windmill and a pond in the entrance to the village.
You stand in a flat terrain spotted with unusual colorful stones.\nYou can see a tower miles away. The village is east of here.
主要
string[] roomsdata = File.ReadAllLines("roomdata.txt");
Console.WriteLine(roomsdata[0])
它将在文本中打印“\n”,而不是实际创建新行。
【问题讨论】:
-
它不会转义任何东西。如果出现“额外”\,可能是在调试视图中查看它(或者是文件的实际内容)..
-
用空行分隔房间描述怎么样?
-
我在文本文件中使用“\n”,当我查看文本的输出时,它变成了“\\n”,它被转义了,如果我尝试做“\\ n”它欺骗了系统,可以这么说它实际上会打印“\\\\n”,添加两个反斜杠以将两者配对放入文本文件中,我不知道为什么。
-
@Alex 这是不正确的(或者,如上所述,正在使用诸如调试器监视窗口之类的东西来查看它,添加“额外”斜杠)。 .NET IO 不“添加转义字符”。请先纠正这个错误的信念/假设。
-
@PeterRuderman 将无法使用空行来拆分每个描述。
标签: c# file-io text-files newline