如何从文本文件中获取特定数据

How to get specific data from text file

我的程序的一部分让用户将他们的记录连同他们的名字一起保存在文本文档中。该程序的工作方式是,如果用户提交自己的名字,他们之前的记录将被取回并重新插入到游戏中。例如,如果我的名字是 Justin,我会在文本框中输入“Justin”,然后程序会查找文本文件,如果它找到一个叫 Justin 的人,那么它会查看接下来的三行数据并将这些行分配给分别为玩家获胜、计算机获胜和平局。但是,我能找到的任何示例都涉及将这些特定数字相加。我希望这里有人可以指出我应该如何构建此代码的正确方向。

private void FileReader(string playername, int playerwins, int computerwins, int ties)
        {
            StreamReader outputfile;
            outputfile = File.OpenText("Records.txt");
            while (!outputfile.EndOfStream)
            {
                if (string.Compare(playername, outputfile.ReadLine()) == 0)
                {
                   //ReadLine() and variable assigning code goes here
                }
            }
        }

如果文本文件足够小(大部分都是),我更愿意一次读写文件的所有行,而不是一次一行。这将“文件处理”与“分数更新”分开,而不是将它们交织在一起。资源也会被自动清理(使用StreamReader,你要记得使用'using'或'Dispose()'来确保发生错误后文件句柄等资源被正确释放)。

对于下面的代码,如果玩家名已经在文件中,则玩家名的分数会被更新。否则,playername 和 playername 的分数将添加到文件末尾。如果该文件尚不存在,则会创建一个新文件。

最后的建议是将该方法重命名为类似 UpdateOrInsertPlayerScores() 的名称。

    private void FileReader(string playername, int playerwins, int computerwins, int ties)
    {
        string filename = "Records.txt";
        string[] lines;

        // read entire text file (if any) into lines
        if (File.Exists(filename))
        {
            lines = File.ReadAllLines(filename);
        }
        else
        {
            lines = new string[0];
        }

        // find playername's line (or -1 if not present)
        int p = -1;
        for (int i = 0; i < lines.Length; ++i)
        {
            if (lines[i] == playername)
            {
                p = i;
                break;
            }
        }
        // update (or insert) playername's scores in lines[]
        if (p == -1)
        {
            // playername does not have scores yet; append 4 new lines representing playername's scores
            List<string> newLines = new List<string>();     // copy lines[] to a List<> so we can add new lines to it
            newLines.AddRange(lines);
            newLines.Add(playername);
            newLines.Add(playerwins.ToString());
            newLines.Add(computerwins.ToString());
            newLines.Add(ties.ToString());
            lines = newLines.ToArray();                     // copy expanded List<> back to lines[]
        }
        else
        {
            // update the 3 lines after playername's line with playername's updated scores

            // verify that the 3 lines to be updated are present after the playername's line P
            if ((p + 3) > (lines.Length - 1))
            {
                throw new Exception("Player scores file is not in the expected format.");
            }
            // update the 3 lines in place
            lines[p + 1] = playerwins.ToString();
            lines[p + 2] = computerwins.ToString();
            lines[p + 3] = ties.ToString();
        }

        // re-write entire text file (with updated lines)
        File.WriteAllLines(filename, lines);
    }