简体   繁体   English

如何从txt文件中仅读取整数

[英]How to read only Integers from txt file

using (BinaryReader reader = 
    new BinaryReader(File.Open(@"Assets\saves\one.txt", FileMode.Open)))
{
    year = reader.ReadInt32();
    month = reader.ReadInt32();
}

I want to get first 2 int values from txt file, the first value is 2018 and the second is 4, but instead of this values both year and month are 577005860. 我想从txt文件中获取前2个int值,第一个值是2018年,第二个值是4,但是代替这个值,年份和月份均为577005860。

You are using BinaryReader , which is attempting to extract the numbers as if they are stored in a flat binary file. 您正在使用BinaryReader ,它试图提取数字,就像它们存储在平面二进制文件中一样。 Read it as a text file instead. 而是将其读取为文本文件。 You should use a StreamReader , and then use Int32.parse to convert the number to an Int32 . 您应该使用StreamReader ,然后使用Int32.parse将数字转换为Int32

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM