让streamreader读取文本文件中的特定行,然后写入控制台c#

时间:2014-06-16 04:07:03

标签: c# streamreader

我有一个包含4个变量类型的结构,该结构的数据保存在文本文件中。结构由问题编号,问题级别,问题和问题答案组成。我试图只将问题打印到控制台窗口,但是atm程序坚持首先编写问题编号,然后是级别,然后是问题,然后是问题答案。我只想将问题打印到控制台屏幕。这就是我到目前为止所拥有的:

static void quiz(QuestionStruct[] _quiz)
{
    bool asked = true;
    int score = 0;
    int AmountAsked = 0;

    string level = "1";
            string ans;
int pos = 0;

var pathToFile = @"..\..\..\Files\questions.txt";
using (StreamReader sr = new StreamReader(pathToFile, true))
{      
    while (AmountAsked < 20 || score >= 50)
    {
        Console.Clear();
        //int pos = 0;
        if (level == "1")
        {

            AmountAsked++;
            questions[pos].Question = sr.ReadLine();
            Console.Write(questions[pos].Question);
            ans = Console.ReadLine();
            if (ans == questions[pos].answer)
            {
                level = "2";
                score = score + 1;
                while (questions[pos].Level != "2")
                {
                    pos++;
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

看起来您没有按正确的顺序阅读文件。您将文件的第一行存储在Question成员中,第二行存储在Answer成员中。然后你永远不会读一个关于问题编号的行。这与文件布局不匹配。

同样很难看到代码中的问题,因为它做了太多事情。您应该重构代码,以便读取文件是一种方法,然后执行测验是另一种方法。这是一个软件设计原则,称为关注点分离http://en.wikipedia.org/wiki/Separation_of_concerns

这是重构的代码。如果您跳过阅读线,或者使用此代码无序读取行,则会更容易发现。

public void Quiz()
{
    List<QuestionStruct> questions = GetQuestions(@"..\..\..\Files\questions.txt");

    RunQuiz(questions);
}


// My job is to parse the file
public List<QuestionStruct> GetQuestions(string pathToFile)
{
    List<QuestionStruct> questions = new List<QuestionStruct>();

    using (StreamReader sr = new StreamReader(pathToFile, true))
    {
        while (!sr.EndOfStream)
        {
            QuestionStruct q = new QuestionStruct();
            q.Number = sr.ReadLine();
            q.Level = sr.ReadLine();
            q.Question = sr.ReadLine();
            q.Answer = sr.ReadLine();

            questions.Add(q);
        }
    }

    return questions;
}


// My job is to run the quiz
public void RunQuiz(List<QuestionStruct> questions)
{

    bool asked = true;
    int score = 0;
    int AmountAsked = 0;

    string level = "1";
    string ans;
    int pos = 0;

    foreach (QuestionStruct question in questions)
    {
        if (!(AmountAsked < 20 || score >= 50))
            break;

        Console.Clear();
        //int pos = 0;
        if (level == "1")
        {
            AmountAsked++;

            Console.Write(question.Question);
            ans = Console.ReadLine();

            if (ans == question.Answer)
            {
                level = "2";
                score = score + 1;
                while (question.Level != "2")
                {
                    pos++;
                }
            }
        }
    }
}

答案 1 :(得分:-1)

如果问题编号,级别,问题和答案都有单独的行,那么您必须在序列中执行4 sr.ReadLine()以将每个正确的行分配给struct的成员。

static void quiz(QuestionStruct[] _quiz)
{
    bool asked = true;
    int score = 0;
    int AmountAsked = 0;

    string level = "1";
    string ans;
    int pos = 0;

    var pathToFile = @"..\..\..\Files\questions.txt";
    using (StreamReader sr = new StreamReader(pathToFile, true))
    {


        while (AmountAsked < 20 || score >= 50)
        {
            Console.Clear();
            //int pos = 0;
           while(!sr.EndOfStream)
            {
            if (level == "1")
            {

                AmountAsked++;
                // Remember the order in which you are storing the values to each member of struch
                questions[pos].number = sr.ReadLine();
                questions[pos].level = sr.ReadLine();
                questions[pos].Question = sr.ReadLine();
                questions[pos].answer = sr.ReadLine();

                Console.Write(questions[pos].Question);
                ans = Console.ReadLine();
                if (ans == questions[pos].answer)
                {
                    level = "2";
                    score = score + 1;
                    while (questions[pos].Level != "2")
                    {
                        pos++;
                    }
                }
            }
          }