【问题标题】:Program that reads file and writes new info读取文件并写入新信息的程序
【发布时间】:2019-05-21 10:23:30
【问题描述】:

所以基本上我有这个包含篮球运动员姓名和身高的文本数据文件,即“Tom is 6 ft”。从这个带有篮球运动员姓名和身高的文本文件中,我正在尝试编写一个代码,该代码贯穿文本数据文件的每一行并将数字与字符串分开,并发现如果一名球员大于 6 英尺,那么该球员已将其发送给团队并将该玩家发送到另一个名为 made it 的文本文件。知道结果已得到解释我在尝试创建代码以将数字与字符串分开并识别玩家身高 6 英尺或以上并将该玩家放入新的文本数据文件时遇到了麻烦。

这是包含程序所需名称和高度的文本数据文件:https://drive.google.com/file/d/10qLuyOzrV2EhFsQ9g4-28rLGIlLFGoDt/view?usp=sharing

现在我已经设法创建了一个程序,它读取文本数据文件并写入另一个文本数据文件,同时还在控制台上逐行显示文本文件中的所有信息。

这是我现在的代码:

using System;

namespace basketball
{
    class Program
    {
    static void Main(string[] args)
    {

        // This section shows how to read a file called Sample.txt stored in the Debug folder of the program folder 
        string fileName = @"Sample.TXT";
        Console.WriteLine("The contents of the file {0} is:", fileName);

        string[] dataFromFile = new string[100];
        int index = 0;

        System.IO.StreamReader streamReader = new System.IO.StreamReader(fileName);
        using (streamReader)
        {
            string fileContents = streamReader.ReadToEnd();

            dataFromFile[index] = fileContents;

            Console.WriteLine(dataFromFile[index]);
            index++;
        }

        Console.WriteLine("Now Line By Line:");
        System.IO.StreamReader reader = new System.IO.StreamReader(fileName);
        using (reader)
        {
            int lineNumber = 0;
            string line = reader.ReadLine();
            while (line != null)
            {
                lineNumber++;
                Console.WriteLine("Line {0}: {1}", lineNumber, line);
                line = reader.ReadLine();
            }
        }

        // This section shows how to write a file called madeit.txt stored in the console programs debug folder 

        string fileName2 = @"madeit.txt";
        System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(fileName2);
        using (streamWriter)
        {
            for (int number = 1; number <= 20; number++)
            {
                streamWriter.WriteLine("This is line number : " + number);
            }
        }
        Console.WriteLine("File is written!");

    }
}
}

这是当前控制台输出的样子,这是一个链接:https://drive.google.com/file/d/13_WKzfVriXlnfRcaqaPWbNFkc4Xix5z2/view?usp=sharing

【问题讨论】:

  • 请将文本文件连同您的问题一起发布。您问题中的链接需要获得访问权限。
  • @preciousbetine 现在应该可以工作了,抱歉

标签: c# file


【解决方案1】:

我建议使用正则表达式。请看这个例子:

    List<string> players = new List<string> {
        @"Grady is 6'1"" ft",
        @"Robert is 5'10"" ft",
        @"Riley is 7 ft",
        @"Sam is 4'9"" ft",
        @"Greg is 6 ft",
        @"Raheem is 6'3"" ft",
        @"Connor is 5'11"" ft"
    };
    const string pattern = @"(.+) is (\d+)('\d+"")? ft";
    var regex = new Regex(pattern);
    foreach (var player in players)
    {
        var match = regex.Match(player);
        if (match.Success)
        {
            bool sixFeetOrTaller = false;
            var name = match.Groups[1].Value;
            var inchesStr = match.Groups[2].Value;
            int inches;
            if (int.TryParse(inchesStr, out inches))
            {
                if (inches >= 6)
                {
                    sixFeetOrTaller = true;
                }
            }
            if (sixFeetOrTaller)
            {
                Console.WriteLine(name + " made it to the team!");
            }
            else
            {
                Console.WriteLine(name + " did not make it to the team");
            }
        }
        else
        {
            Console.WriteLine("Unable to parse line " + player);
        }
    }

输出:

Grady made it to the team!
Robert did not make it to the team
Riley made it to the team!
Sam did not make it to the team
Greg made it to the team!
Raheem made it to the team!
Connor did not make it to the team

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 2023-04-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2020-01-31
    • 2011-11-29
    相关资源
    最近更新 更多