A text file is not getting displayed to the console in c# [duplicate]

亡梦爱人 提交于 2021-02-11 13:36:08

问题


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


回答1:


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.




回答2:


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();


来源:https://stackoverflow.com/questions/59166980/a-text-file-is-not-getting-displayed-to-the-console-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!