0

我正在制作一个小型益智游戏,并尝试使用 StringReader 将一些信息从文本文件加载到字符串中。它将加载到数据网格视图。该文本文件名为 TextFile1.txt,位于名为 Puzzles 的文件夹中。文本文件设置为始终复制到输出目录。

项目构建但不会加载数据网格视图中的项目。文本文件内容如下

x|y|direction|number|word|clue
5|5|down|1|love|Let _____ Rule
4|5|across|2|closed|Not Open
5|8|across|3|eraser|At the other end of a pencil
10|8|down|2|red|Hunt for _____ October
10|10|across|4|dallas|Redskin rival's city
9|5|down|3|dare|Triple Dog
13|8|down|4|relapse|To succumb again
11|12|across|5|cap|A night ____

代码

Clues clue_window = new Clues();
    List<id_cells> idc = new List<id_cells>();
    public String puzzle_file = Application.StartupPath + "\\Puzzles\\TextFile1.txt";


    public Form1()
    {
        buildWordList();
        InitializeComponent();
    }



    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void buildWordList()
    {
        String line = "";
        using (StringReader s = new StringReader(puzzle_file))
        {
            s.ReadToEnd();
            line = s.ReadLine();//ignores the first line
            while((line = s.ReadLine()) != null)
            {
                String[] l = line.Split('|');
                idc.Add(new id_cells(Int32.Parse(l[0]), Int32.Parse(l[1]), l[2], l[3], l[4], l[5]));
                clue_window.clue_table.Rows.Add(new String[] { l[3], l[2], l[5] });
            }
        }
    }
4

1 回答 1

-1

首先,当您遇到此类问题时,您首先检查 Reader 的路径(我建议您使用 StreamReader 而不是 StringReader)。

从我的角度来看,有两个问题可能是:

1.读取可能无法正常工作,因为没有找到它的txt文件。(当它进入该循环时显示一个消息框框以检查)

或者

2. 那 s.ReadToEnd 从流中读取所有内容。

该函数从文件中复制所有数据(从第一个字节到最后一个字节)。因此,下次您读取一行时,它将返回 null。

于 2018-06-27T23:01:15.100 回答