【问题标题】:Updating the file text streamreader or streamwriter更新文件文本 streamreader 或 streamwriter
【发布时间】:2013-12-01 15:53:44
【问题描述】:

我有一个文件有问题。当我按下一个按钮时,它会跳到下一个未解决的问题。

我想做一个算法,当我按下按钮时,它会删除问题文件中当前问题的行

就像我有 10 个问题一样。我回答第一个,我跳过另一个。当我在最后一个问题上并按下跳过键时,我想进入第二个问题。我想删除已解决问题的行。

我该怎么做?

这是我试过的

public partial class Form1 : Form
{

    Label[,] answerLabel = new Label[1, 3];

    StreamReader question = new StreamReader(Application.StartupPath + "\\intrebari.txt");
    StreamReader answer1 = new StreamReader(Application.StartupPath + "\\raspuns1.txt");
    StreamReader answer2 = new StreamReader(Application.StartupPath + "\\raspuns2.txt");
    StreamReader answer3 = new StreamReader(Application.StartupPath + "\\raspuns3.txt");
    StreamReader goodAnswer = new StreamReader(Application.StartupPath + "\\raspunsbun.txt");

    int answersNo;
    int currentQuestion;

    int totalNo;
    bool[] Checked = new bool[26];
    int score;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        totalNo = 26;
        answersNo = 0;
        currentQuestion = 0;

        score = 0;

        for (int i = 0; i < 1; i++)
            for (int j = 0; j < 3; j++)
            {
                answerLabel[i, j] = new Label();
                answerLabel[i, j].Left = 50;
                answerLabel[i, j].AutoSize = false;
                answerLabel[i, j].BorderStyle = BorderStyle.FixedSingle;
                answerLabel[i, j].Width = 500;
                answerLabel[i, j].Height = 45;
                answerLabel[i, j].Top = 200 +j * 50;
                answerLabel[i, j].BackColor = Color.White;
                this.Controls.Add(answerLabel[i, j]);
            }

        label1.Text = question.ReadLine();
        answerLabel[0, 0].Text = answer1.ReadLine();
        answerLabel[0, 1].Text = answer2.ReadLine();
        answerLabel[0, 2].Text = answer3.ReadLine();

        answerLabel[0, 0].Click += new EventHandler(answer1_Click);
        answerLabel[0, 1].Click += new EventHandler(answer2_Click);
        answerLabel[0, 2].Click += new EventHandler(answer3_Click);

        for (int i = 0; i < totalNo; i++)
        {
            Checked[i] = false;
        }



    }

    private void button1_Click(object sender, EventArgs e) // verifica
    {
        Checked[currentQuestion] = true;

        if (answerLabel[0, 0].BackColor == Color.Yellow && answerLabel[0, 1].BackColor == Color.White && answerLabel[0, 2].BackColor == Color.White)
        {
            if (File.ReadLines(Application.StartupPath + "\\raspunsbun.txt").ElementAt(currentQuestion) == answerLabel[0, 0].Text)
            {
                MessageBox.Show("bravo");
                score++;
                //MessageBox.Show(score.ToString());
            }
            else
            {
                MessageBox.Show("ai gresit!");
            }
        }
        else
        {
            if (answerLabel[0, 1].BackColor == Color.Yellow && answerLabel[0, 0].BackColor == Color.White && answerLabel[0, 2].BackColor == Color.White)
            {
                if (File.ReadLines(Application.StartupPath + "\\raspunsbun.txt").ElementAt(currentQuestion) == answerLabel[0, 1].Text)
                {
                    MessageBox.Show("bravo");
                    score++;
                    //MessageBox.Show(score.ToString());
                }
                else
                {
                    MessageBox.Show("ai gresit!");
                }
            }
            else
            {
                if (answerLabel[0, 2].BackColor == Color.Yellow && answerLabel[0, 1].BackColor == Color.White && answerLabel[0, 0].BackColor == Color.White)
                {
                    if (File.ReadLines(Application.StartupPath + "\\raspunsbun.txt").ElementAt(currentQuestion) == answerLabel[0, 2].Text)
                    {
                        MessageBox.Show("bravo");
                        score++;
                       // MessageBox.Show(score.ToString());
                    }
                    else
                    {
                        MessageBox.Show("ai gresit!");
                    }
                }
            }
        }

        if (answersNo < totalNo - 1)
        {
            do
            {
                if (currentQuestion < totalNo - 1)
                {
                    currentQuestion++;
                    MessageBox.Show(currentQuestion.ToString());
                    label1.Text = question.ReadLine();
                    answerLabel[0, 0].Text = answer1.ReadLine();
                    answerLabel[0, 1].Text = answer2.ReadLine();
                    answerLabel[0, 2].Text = answer3.ReadLine();
                    break;
                }
                else
                {
                    currentQuestion = 0;
                    question.BaseStream.Position = 0;
                    question.DiscardBufferedData();
                    answer1.BaseStream.Position = 0;
                    answer1.DiscardBufferedData();
                    answer2.BaseStream.Position = 0;
                    answer2.DiscardBufferedData();
                    answer3.BaseStream.Position = 0;
                    answer3.DiscardBufferedData();

                }
            }
            while (Checked[currentQuestion] == false);

        }
        else
        {
            MessageBox.Show("Ati terminat testul !");
        }

        answersNo++;

    }

    private void answer1_Click(object sender, EventArgs e) 
    {
        answerLabel[0, 0].BackColor = Color.Yellow;
    }
    private void answer2_Click(object sender, EventArgs e) 
    {
        answerLabel[0, 1].BackColor = Color.Yellow;
    }
    private void answer3_Click(object sender, EventArgs e) 
    {
        answerLabel[0, 2].BackColor = Color.Yellow;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //sterge raspunsul
        for (int i = 0; i < 1; i++)
            for (int j = 0; j < 3; j++)
            {
                answerLabel[i, j].BackColor = Color.White;
            }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        //raspunde mai tarziu 


        if (answersNo < totalNo - 1)
        {
            do
            {
                if (currentQuestion < totalNo - 1)
                {
                    currentQuestion++;
                    break;
                }
                else
                {
                    currentQuestion = 0;
                    question.BaseStream.Position = 0;
                    question.DiscardBufferedData();
                    answer1.BaseStream.Position = 0;
                    answer1.DiscardBufferedData();
                    answer2.BaseStream.Position = 0;
                    answer2.DiscardBufferedData();
                    answer3.BaseStream.Position = 0;
                    answer3.DiscardBufferedData();
                }
            }
            while (Checked[currentQuestion] == true);

            label1.Text = question.ReadLine();
            answerLabel[0, 0].Text = answer1.ReadLine();
            answerLabel[0, 1].Text = answer2.ReadLine();
            answerLabel[0, 2].Text = answer3.ReadLine();
        }

    } 
}

【问题讨论】:

  • 需要更多 clarificationcodetemplate of your questions file
  • 我发布了我尝试过的内容。

标签: c# winforms file-io


【解决方案1】:

我不明白您通过逐行删除问题行来达到什么目的。 我希望您希望用户在他之前结束的下一个问题上继续测试?

我建议在申请结束时将所有未回答的问题写到临时文件中:

Files.WriteAllLine(tempFile, unansweredQuestion[]);

并将问题文件替换为临时文件:

File.Delete("questions.txt");
File.Move(tempFile, "questions.txt");

【讨论】:

    【解决方案2】:

    尝试使用 XML 存储您的问题,然后根据需要使用 LINQ to XML 加载、组织和删除问题。 Here 是一篇很棒的文章,其中包含许多 LINQ to XML 示例。如果您是 XML 新手,W3Schools 有一些很棒的 XML 教程here

    【讨论】:

      猜你喜欢
      • 2012-02-22
      • 2015-10-27
      • 2019-02-26
      • 2013-05-15
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      相关资源
      最近更新 更多