文本文件未在C#中显示到控制台 [英] A text file is not getting displayed to the console in c#

查看:45
本文介绍了文本文件未在C#中显示到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用C#制作琐事游戏.我想为问题和答案加载一个简单的.txt文件.但是我似乎无法将问题显示在控制台上.这是在.txt文件中加载的代码

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

文本文件看起来像这样

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.

谢谢

推荐答案

您将要使用 StreamReader ,而不是 StringReader (通过 File.OpenRead 或使用文件路径构造函数):

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

StringReader 在所传递的字符串上为您提供了 TextReader ,在您的情况下为文件名,而不是文件内容.

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

或者,如@Rufus L所指出的那样,您可以使用 File.ReadAllLines ,它将为您提供所有在内存中的字符串数组,而不是像当前一样流式传输.

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.

这篇关于文本文件未在C#中显示到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆