[英]Problems with reading, overwriting and showing a txt file C#
I got some problems with reading from a txt file. 从txt文件读取时出现一些问题。 My proj.
我的项目 it's kind of a game, which is able to record highscores, show them and reset them aswell.
这是一种游戏,可以记录高分,显示高分并重设高分。 The problem is that if I reset the highscores (via a ToolStripMenuItem from the main form), the higscores form stops updating.
问题是,如果我重置了高分(通过主窗体中的ToolStripMenuItem),则higscores窗体将停止更新。
For example, a step-by-step approach: 例如,分步方法:
- open app (via debugger) (I mention that the .txt is clean)
打开应用程序(通过调试器)(我提到.txt很干净)
- play a game, record a highscore
玩游戏,取得高分
- see the highscores form, my highscore is there
看到高分表格,我的高分在那里
- reset the highscores
重置高分
- open the highscore form again, my highscore is still there.
再次打开高分表格,我的高分仍然存在。 (but it is cleared from the .txt file, because if I reopen the game and open de highscore form again, it is clean)
(但已从.txt文件中清除了该文件,因为如果我重新打开游戏并再次打开de highscore表单,它就很干净)
If I do the steps like 1-2-4-3, when i see the highscores form, it is clean. 如果我执行1-2-4-3之类的步骤,当我看到高分表格时,它就干净了。
Some codes: The reset button works like this: 一些代码:重置按钮的工作方式如下:
private void resetToolStripMenuItem1_Click(object sender, EventArgs e)
{
DialogResult reset = MessageBox.Show("Are you sure you want to reset the highscores?", "Reset highscores", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (reset == DialogResult.OK)
{
File.WriteAllText("highscores.txt", string.Empty);
MessageBox.Show("Successfully reset highscores");
}
}
The highscores form_load is like this: 高分form_load如下所示:
StreamReader inputFile = File.OpenText("highscores.txt");
easy = 0; med = 0; hard = 0;
while ((x = inputFile.ReadLine()) != null)
{
string[] words = x.Split('*');
m = 0;
foreach (string s in words)
{
// here comes some code which keeps one highscore in memory
}
// here is that one highscore sorted by difficulty
}
inputFile.Close();
//here are the highscores sorted
I didn't write all the code because it was too long, and I believe, irrelevant. 我没有编写所有代码,因为它太长了,我认为这是无关紧要的。 Here is the writing of highscores in the txt: (when completing the game, a new form appears with a textbox and a button, here is the code:)
这是txt中高分的写法:(完成游戏时,将出现一个带有文本框和按钮的新表格,这是代码:)
if (DialogResult == DialogResult.OK)
{
while (ok == 0)
{
string s = textBox1.Text;
ok = 1;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '*') ok = 0;
}
if (ok == 0)
{
MessageBox.Show("Your name must not contain the '*' character");
textBox1.Text = "";
}
}
StreamWriter outputFile = File.AppendText("highscores.txt");
outputFile.WriteLine(Form1.dificultate + "*" + Form1.secunde.ToString() + "*" + Form1.moves + "*" + textBox1.Text);
outputFile.Close();
}
You can add the event OnVisibleChanged
to the form, (Docs here ), and inside that, load the high-score file. 您可以将事件
OnVisibleChanged
添加到表单( 此处为 Docs)中,并在其中加载高分文件。
To avoid reloading the high-score list everytime the highscore form is shown(if that is a problem), you can set a global flag when resetting the highscore list, and this flag can decide whether to reload the file again. 为了避免每次显示高分表单时都重新加载高分列表(如果有问题),可以在重置高分列表时设置全局标志,并且该标志可以决定是否再次重新加载文件。
As @Hans Passant said, yhe problem was that the for(;;) loop didn't run while the txt was empty. 正如@Hans Passant所说,问题是在txt为空时for(;;)循环没有运行。 (after it was reset).
(重置后)。
while ((x = inputFile.ReadLine()) != null)
{
c++;
//reading and keeping in memory the highscores
}
if(c==0) //make all the highscore labels empty, skip sorting the highscores
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.