【问题标题】:Is there a way to convert Stream.Read() to int?有没有办法将 Stream.Read() 转换为 int?
【发布时间】:2021-01-14 10:24:36
【问题描述】:

我正在处理我的学校项目,并且遇到了数据类型的问题。 起初我将数据写入 txt 文件,然后我想读取它以便对数据进行排序。但是用字符串似乎不可能做到这一点。如果需要,这里有一些代码

string fileName = "highscore.txt";
                int textToAdd = Score;
                using (StreamWriter writer = new StreamWriter(fileName, true))
                {
                    writer.Write(textToAdd+"\n");

                }
                using (StreamReader reader = new StreamReader(fileName, true))
                {
                    

                }

文本包含多个用空格分隔的数字,例如:

1 2 3 

我想读取数字并对其进行排序。

【问题讨论】:

  • 我可以建议改为编写(和阅读)JSON 吗?
  • 任何建议如何做到这一点?我的意思是它会好还是完全矫枉过正。完全没有使用 json 的经验 :(
  • 谢谢@Liam 我会试试的
  • @Liam 的 stackoverflow.com/questions/13297563/… 是一个很好的起点。文件解析很难。 JSON 处理要容易得多。
  • 这能回答你的问题吗? Read and parse a Json File in C#

标签: c# monogame


【解决方案1】:

希望下面的sn-p对你有帮助!

using (StreamReader reader = new StreamReader(fileName, true))
{
     int value = Convert.ToInt32(reader.ReadToEnd());
}

【讨论】:

  • File.ReadAllText(fileName) 有点整洁
  • 如果他想逐行阅读,我同意你的看法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 2020-05-02
  • 1970-01-01
  • 2014-11-06
  • 2017-03-25
  • 2021-09-01
  • 2021-05-15
相关资源
最近更新 更多