保存到文件折断-C#

发布于 2025-01-22 06:27:35 字数 1354 浏览 2 评论 0 原文

我尝试使用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");
}

I try to use a txt file as a backup: when a checkbox is activated, we add 1 in a "victory" txt file and when it is deactivated we add 1 in a "loose" txt file, and here is my problem:
For example if the checkbox is disabled, I run the script and I get 1 in "loose" and 0 in "victory", so normal; but if I run it again (without changing anything), I get 51 in "loose" and 49 in "victory", but I would like to get 2 and 0..
I don't know if it's really clear;

here is the piece of code:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2025-01-29 06:27:35

读取读取单个字符,而不是字符串。 ASCII 1 的数字表示为 49 。将 2 添加到它(无论您添加 1 ,无论复选框的状态如何)使 51 写回到一个文件。

0 由Numeral 48 表示,您将其添加到1,然后回信 - 因此 49

您应该看一下 streamReader.Readline 作为阅读第一个字符仅意味着您在10次运行程序时,为了胜利或损失,它将再次回到1,而不是10。

Read reads a single character, not a string. The numeral representation of ASCII 1 is 49. Add 2 to that (which you do on every read since you add 1 regardless of the state of the checkbox) makes 51 be written back to one file.

0 is represented by numeral 48, which you add 1 to and then write back - thus 49.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文