c# - Reading a text file and assigning certain lines to certain variables -
I am working on a Trivia C # console game that reads a text file that has a question, multiple choice Answer, number for correct choice and answer explanation. There are four questions set. The task is to prompt each question on the console window in the file, enter the user's answer and then compare one with the correct answer of the user. It should be done for this
All four questions in the file .
A class created with questions, multiple and war chauces, correct answers and answer exploration, gates and sets for each field.
What I have to fight after I read the file, to specify each question in a certain variable to indicate the user and also to do it for four sets I have size 4 To create an array (as I have 4 lines for each question set), to store the lines, each element is specified in each variable above, but I did not understand that How loop for all four sets to.
QuestionUnit is a class in which the question is in the field.
Public Zero ReadQuestionFile (QuestionUnit unit) {string [] arrayReader = new string [4]; String line = ""; Int i = 0; String fileName = "TextFile1.txt"; Stream reader myReader = new streamreader (filename); While ((line = myReader.ReadLine ())! = Null & amp; i & lt; 4) {arrayReader [i] = line; // console.light line (line); //Console.WriteLine (arrayReader[i)); I ++; } Unit.M_Question = arrayReader [0]; Unit.M_Answers = arrayReader [1]; Unit.M_CorrectAnswers = arrayReader [2]; Unit.M_Explanation = array reader [3]; } Any ideas about doing this?
Because all your data is in one file, you need to take a slightly different approach. Instead of going to item to fill, I need to return the list of questions reading the function:
Public IEnumerable & lt; QuestionUnit & gt; ReadQuestionFile () Then, until you reach the end of the file, read in a loop. This method is not safe for invalid input, so be careful:
string fileName = "TextFile1.txt"; & Lt; QuestionUnit & gt; ReadQuestions = New list & lt; Question Unit & gt; (); (StreamReader myReader = New StreamReader (fileName)) using {while (MyReader.EndOfStream) {QuestionUnit newQuestion = new QuestionUnit (); NewQuestion.M_Question = myReader.ReadLine (); NewQuestion.M_Answers = myReader.ReadLine (); new query. M_CorrectAnswers = myReader.ReadLine (); NewQuestion.M_Explanation = myReader.ReadLine (); ReadQuestions.Add (newQuestion); }} Read Read Questions; Basically, you read up to the end of the file, reading four lines at a time. If the input format is not correct then you will get some blank values, you do not really need an array because you can store values directly in your object, which you then add to the list when you make it popular (technically You could do it before). Then you return the loaded list that you use it.
You can use yield returns instead of directly adding it to a list, but to start out an advanced concept, and I'm not sure that the file It is good to know about how well it will be with / O. Only one change will remove readquestions , and line: yield returns new question; Where the call add is currently. I know that I can clarify anything!
Comments
Post a Comment