保存到文件折断-C#
我尝试使用txt文件作为备份:激活复选框时,我们在“胜利” txt文件中添加1,当它停用时,我们在“松散” TXT文件中添加了1个,这是我的问题: 例如,如果将复选框禁用,我运行脚本,然后在“松散”中获得1,而在“胜利”中获得0,所以很正常;但是,如果我再次运行(没有任何更改),我会在“松散”中获得51,而在“胜利”中获得49,但我想获得2和0 .. 我不知道这是否真的很清楚;
这是代码:
if (Directory.Exists("C:/PekmiIndustries/BrokenGame"))
{
TextReader VtxtR = new StreamReader("C:/PekmiIndustries/BrokenGame/Victory.txt");
TempVReadStr = VtxtR.Read().ToString();
VRead = Convert.ToInt32(TempVReadStr);
VRead += 1;
if (VictoireCheck.Checked == true)
{
VRead += 1;
}
VtxtR.Close();
TextWriter VtxtW = new StreamWriter("C:/PekmiIndustries/BrokenGame/Victory.txt");
VtxtW.Write(VRead.ToString());
VtxtW.Close();
TextReader LtxtR = new StreamReader("C:/PekmiIndustries/BrokenGame/Loose.txt");
TempLReadStr = LtxtR.Read().ToString();
LRead = Convert.ToInt32(TempLReadStr);
LRead += 1;
if (VictoireCheck.Checked == false)
{
LRead += 1;
}
LtxtR.Close();
TextWriter LtxtW = new StreamWriter("C:/PekmiIndustries/BrokenGame/Loose.txt");
LtxtW.Write(LRead.ToString());
LtxtW.Close();
MessageBox.Show("Les stats ont correctement étés sauvegardées !", "BrokenGame");
}
else
{
MessageBox.Show("Err Sauvegarde - Dossier; \nRéessaie de sauvegarder les stats, ou de redémarrer BrokenGame", "BrokenGame");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
读取
读取单个字符,而不是字符串。 ASCII1
的数字表示为49
。将2
添加到它(无论您添加1
,无论复选框的状态如何)使51
写回到一个文件。0
由Numeral48
表示,您将其添加到1,然后回信 - 因此49
。您应该看一下 streamReader.Readline 作为阅读第一个字符仅意味着您在10次运行程序时,为了胜利或损失,它将再次回到1,而不是10。
Read
reads a single character, not a string. The numeral representation of ASCII1
is49
. Add2
to that (which you do on every read since you add1
regardless of the state of the checkbox) makes51
be written back to one file.0
is represented by numeral48
, which you add 1 to and then write back - thus49
.You should take a look at
StreamReader.ReadLine
as reading the first character only would mean that when you've run the program 10 times, for either victory or loss, it would overflow back to 1 again, not 10.