Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 131465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:06:35+00:00 2026-05-11T06:06:35+00:00

Were trying to use external file (txt or CSV) in order to create a

  • 0

Were trying to use external file (txt or CSV) in order to create a file stream in C#. The data in the file is that of a quiz game made of :

1 short question 4 possibles answers 1 correct answer

The program should be able to tell the user whether he answered correctly or not.

I’m looking for an example code/algorithm/tutorial on how to use the data in the external file to create a simple quiz in C#. Also, any suggestions on how to construct the txt file (how do I remark an answer as the correct one?). Any suggestions or links? Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:06:35+00:00Added an answer on May 11, 2026 at 6:06 am

    There’s really no set way to do this, though I would agree that for a simple database of quiz questions, text files would probably be your best option (as opposed to XML or a proper database, though the former wouldn’t be completely overkill).

    Here’s a little example of a text-based format for a set of quiz questions, and a method to read the questions into code. Edit: I’ve tried to make it as easy as possible to follow now (using simple constructions), with plenty of comments!

    File Format

    Example file contents.

     Question text for 1st question... Answer 1 Answer 2 !Answer 3 (correct answer) Answer 4  Question text for 2nd question... !Answer 1 (correct answer) Answer 2 Answer 3 Answer 4 

    Code

    This is just a simple structure for storing each question in code:

    struct Question {     public string QuestionText; // Actual question text.     public string[] Choices;    // Array of answers from which user can choose.     public int Answer;          // Index of correct answer within Choices. } 

    You can then read the questions from the file using the following code. There’s nothing special going on here other than the object initializer (basically this just allows you to set variables/properties of an object at the same time as you create it).

    // Create new list to store all questions. var questions = new List<Question>();  // Open file containing quiz questions using StreamReader, which allows you to read text from files easily. using (var quizFileReader = new System.IO.StreamReader('questions.txt')) {     string line;     Question question;      // Loop through the lines of the file until there are no more (the ReadLine function return null at this point).     // Note that the ReadLine called here only reads question texts (first line of a question), while other calls to ReadLine read the choices.     while ((line = quizFileReader.ReadLine()) != null)     {         // Skip this loop if the line is empty.         if (line.Length == 0)             continue;          // Create a new question object.         // The 'object initializer' construct is used here by including { } after the constructor to set variables.         question = new Question()         {             // Set the question text to the line just read.             QuestionText = line,             // Set the choices to an array containing the next 4 lines read from the file.             Choices = new string[]             {                  quizFileReader.ReadLine(),                  quizFileReader.ReadLine(),                 quizFileReader.ReadLine(),                 quizFileReader.ReadLine()             }         };          // Initially set the correct answer to -1, which means that no choice marked as correct has yet been found.         question.Answer = -1;          // Check each choice to see if it begins with the '!' char (marked as correct).         for(int i = 0; i < 4; i++)         {             if (question.Choices[i].StartsWith('!'))             {                 // Current choice is marked as correct. Therefore remove the '!' from the start of the text and store the index of this choice as the correct answer.                 question.Choices[i] = question.Choices[i].Substring(1);                 question.Answer = i;                 break; // Stop looking through the choices.             }         }          // Check if none of the choices was marked as correct. If this is the case, we throw an exception and then stop processing.         // Note: this is only basic error handling (not very robust) which you may want to later improve.         if (question.Answer == -1)         {             throw new InvalidOperationException(                 'No correct answer was specified for the following question.\r\n\r\n' + question.QuestionText);         }          // Finally, add the question to the complete list of questions.         questions.Add(question);     } } 

    Of course, this code is rather quick and basic (certainly needs some better error handling), but it should at least illustrate a simple method you might want to use. I do think text files would be a nice way to implement a simple system such as this because of their human readability (XML would be a bit too verbose in this situation, IMO), and additionally they’re about as easy to parse as XML files. Hope this gets you started anyway…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create and use jar file in an Android project under
I'm trying to use 7-Zip to zip up a file via the system() function
I'm trying to use JQuery's $.getJSON to access Model data from the Controller, and
I'm trying to use an external library DevExpress.XtraTreeList.v8.1.dll in my vsto office addin built
I'm trying to use Grails' built-in mechanism for loading external configuration files (*.groovy and
I'm trying to create a script that will delete all user properties for a
I'm trying to deploy an application written in wpf c#. I use an external
I am trying to create a permanent wmi event consumer that will wait for
I am trying to bind events from a GUI file to use code from
I'm trying to use one of the simplest forms of reflection to create an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.