【发布时间】:2019-02-13 12:15:14
【问题描述】:
我正在编写一个关于求职面试的程序。一切正常,除了一件事。当我使用外部方法 TotalLines(我有单独的 StreamReader)时,它工作正常,但是当我计算程序中的 totalLines 数量时,我在第一个问题的开头收到一个问号。就是这样:
?你叫什么名字?
但在我正在阅读的文本文件中,我只是 - 你叫什么名字?
我不知道为什么会这样。也许是我将 StreamReader 返回到开始的问题?我检查了我的编码,一切,但没有任何效果。谢谢你的帮助:)
PotentialEmployee potentialEmployee = new PotentialEmployee();
using (StreamReader InterviewQuestions = new StreamReader(text, Encoding.Unicode))
{
int totalLines = 0;
while (InterviewQuestions.ReadLine() != null)
{
totalLines++;
}
InterviewQuestions.DiscardBufferedData();
InterviewQuestions.BaseStream.Seek(0, SeekOrigin.Begin);
for (int numberOfQuestions = 0; numberOfQuestions < totalLines; numberOfQuestions++)
{
string question = InterviewQuestions.ReadLine();
Console.WriteLine(question);
string response = Console.ReadLine();
potentialEmployee.Responses.Add(question, response);
}
}
但是当我在外部方法中进行 TotalLines 计算时,不会显示问号。有什么想法吗?
【问题讨论】:
-
这是由于编码。使用记事本打开文件并在打开的弹出窗口中选择 unicode 而不是 ANSI(默认)。
-
@jdweng:我认为这更有可能是由于 BOM 而不是编码。
-
@jdweng 不是真的,我刚刚将编码更改为 Unicode、UTF-8、ANSI,问号还在。
标签: c# streamreader