0

I am working on trying to make a trivia game in c#. I want to load in a simple .txt file for the questions and the answers. But I can't seem to get the questions to display to the console. Here is the code for loading in the .txt file

static void LoadData(string filename)
    {
        try
        {
            using(StringReader reader = new StringReader(filename))
            {
                string line;

                while((line = reader.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("File could not be read");
            Console.WriteLine(e.Message);
        }
    }

Here is the code where I am trying to display the questions to the console. Right now all that happens is the location of the text file gets displayed but nothing else.

 static void Main(string[] args)
    {
        string filename = @"C:\Trivia\questions.txt";

        LoadData(filename);
    }

The text file looks like this

What is another name for SuperMan?
What is Superman's only weakness?
What is the name of Batman's secret identity?
Batman protects what city?
How did Spiderman get his superpowers?
This superheros tools include a bullet-proof braclets and a magic lasso.Who is she?
Which superhero has an indestructible sheild?
Which superhero cannot transformback into human form?
What villan got his distinctive appearance form toxic chemicals?
What is the name of the archnemesis of the Fantastic Four?    

I am really unsure on what I am doing wrong. Any suggestions would be appreciated.

Thanks

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Cole.C
  • 45
  • 5
  • 1
    Or you can use a single line: `Console.WriteLine(File.ReadAllText(filename));` The `System.IO.File` class has many helpful static methods that can be used for reading all the file as a string (as above), or as a `List`, using `ReadAllLines`, as well as methods for writing to files. It's a handy wrapper around the "reader" and "writer" classes. – Rufus L Dec 03 '19 at 23:52
  • If you have a new question - ask new one and do not edit the existing question to be completely different one. I rolled back your change as it puts a new question into the existing one and (more importantly) invalidates existing answers. (The question itself actually has canonical Q&A showing all sorts of ways to read text file... ) – Alexei Levenkov Dec 04 '19 at 00:29

2 Answers2

2

You'll want to use StreamReader, rather than a StringReader (via File.OpenRead or with the file path constructor):

static void LoadData(string filename)
{
    try
    {
        using (StreamReader reader = new StreamReader(filename))
        {
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("File could not be read");
        Console.WriteLine(e.Message);
    }
}

A StringReader gives you a TextReader over the string you pass in, in your case the filename, rather than the files contents.

Alternatively as pointed out by @Rufus L, you could use File.ReadAllLines which will give you an array of strings all in-memory rather than streaming it as you are currently.

Stuart
  • 5,358
  • 19
  • 28
  • Ahh that makes sense. Thanks so much. I have run into another issue now I have updated the original question. – Cole.C Dec 04 '19 at 00:01
  • Happy to help. `string.Split` wants a `char`, so you'll need to quote the comma with single quotes, so ` rather than " – Stuart Dec 04 '19 at 00:06
  • Ok, tried that but then I get this message at the beginning `File could be read index was outside of the bounds of the array` but then the text file gets read and everything gets displayed. – Cole.C Dec 04 '19 at 00:11
0

Try Using StreamReader instared of stringreader

using (StreamWriter text= new StreamWriter(@"file.txt"))
{
    text.WriteLine("The first line");
    text.WriteLine("This text is on the second line");
    text.WriteLine("And the third one.");
    text.Flush();
}
Console.WriteLine("The file has been successfully written.");

// appending a text to the file
using (StreamWriter sw = new StreamWriter(@"file.txt", true))
{
    sw.WriteLine("An appended line");
    sw.Flush();
}
Console.WriteLine("A new line has been successfully appended into the file.");

// printing the contents of the file
Console.WriteLine("Printing file contents:");

using (StreamReader sr = new StreamReader(@"file.txt"))
{
    string s;
    while ((s = sr.ReadLine()) != null)
    {
        Console.WriteLine(s);
    }
}
Console.ReadKey();