【问题标题】:Ordering writeline outputs from text file using C# in console在控制台中使用 C# 从文本文件中排序 writeline 输出
【发布时间】:2016-06-10 11:25:36
【问题描述】:

我想知道你是否可以帮助我。本质上,我的程序必须扫描一个文本文件,然后打印这些行。如果可能,打印的每一行也必须按字母顺序排列。我可以通过 cmd 指向任何文件,而不是自动将其指向特定文件和特定位置。

我有这个,因为我想让事情以基本形式运行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Program
{
class Program
{
    static void Main(string[] args)
    {
        String line;
        try
        {
            //We Have to pass the file path and packages.txt filename to the StreamReader constructor
            StreamReader sr = new StreamReader("D:\\Users\\James\\Desktop\\packages.txt");

            //Instruction to read the first line of text
            line = sr.ReadLine();

            //Further Instruction is to to read until you reach end of file
            while (line != null)
            {
                //Instruction to write the line to console window
                Console.WriteLine(line);
                //The read the next line
                line = sr.ReadLine();
            }

            //Finally close the file
            sr.Close();
            Console.ReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
        finally
        {
            Console.WriteLine("Executing finally block.");
        }
    }
}
}

希望大家帮帮我,我很生疏!

我的想法是将字符串转换为 char 数组?然后使用 array.sort 方法修改和排序。

好的,伙计们。根据您的建议,我做了一些更改。我现在收到一个异常,因为我们正试图让它接受一个参数,以便我们将它指向任何文本文件,而不是特定文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Program
{
class Program
{
    static void Main (params string[] args)
    {
        string PathToFile = args[1];
        string TargetPackages = args[2];

        try

        {

            string[] textLines = File.ReadAllLines(PathToFile);
            List<string> results = new List<string>();

            foreach (string line in textLines)
            {
                if (line.Contains(TargetPackages))
                {
                    results.Add(line);
                }

                Console.WriteLine(results);
            }
        }


        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
        finally
        {
            Console.WriteLine("Executing finally block.");
        }
    }
}
}

【问题讨论】:

  • 您的意思是“按字母顺序排列”一行中的每个字符都按字母顺序排序吗?
  • 感谢您指出这一点。我的意思是实际上每一行都由一个双空格分隔。每个句子的结构是 word -> word 。所以我基本上需要确保第一个单词是按字母顺序排序的。
  • 是否也需要以同样的方式输出? : "dorw -> 单词"
  • 我想最好举个例子。假设有三行首先是 dog -> word 第二行 cat -> word 第三行 Mouse -> word 输出应该是三行,但按字母顺序排列(按第一个单词,即 cat 然后 dog 然后 mouse
  • 好的,在我编辑了我的帖子后得到了这些新信息。可能这次更适合您的问题。

标签: c# arrays string console.writeline


【解决方案1】:

如果只想按第一个单词排序并输出,则需要将所有行读入内存(希望你的文件不要太大),对行进行排序,然后将它们写回。

有很多方法可以做到这一点。 File class 有一些帮助函数,使得逐行读取和写入文本文件变得非常简单,而 LINQ 的 OrderBy 方法可以快速排序。

File.WriteAllLines(
    outputFileName,
    File.ReadLines(inputFileName).OrderBy(line => line));

请参阅File.WriteAllLinesFile.ReadLines,了解它们的工作原理。

如果要加载每一行,对第一个单词进行排序,然后重新输出该行:

File.WriteAllLines(
    outputFileName,
    File.ReadLines(inputFileName)
        .Select(line =>
            {
                var splits = line.Split(new [] {' '}};
                var firstWord = new string(splits[0].OrderBy(c => c));
                var newLine = firstWord + line.Substring(firstWord.Length);
                return newLine;
            }));

请注意,这一次加载和处理一行,因此您不必将整个文件保存在内存中。

【讨论】:

  • 谢谢。你们都很有帮助,我会试试看的!
【解决方案2】:

最好一次阅读所有行,然后分别查看每一行,如下所示:

List<string> allLines = System.IO.File.ReadLines(pathToFile).ToList();
allLines = allLines.OrderBy(line => line).ToList(); //this orders alphabetically all your lines
foreach (string line in allLines)
{
    Console.WriteLine(line);
}

这将按字母顺序打印文件中的所有行。

您还可以使用打开应用程序时收到的 args 参数对路径进行参数化:

CMD> pathToYourExe.exe path1 path2 path3

您可以使用项目调试菜单中的 DEBUG 参数来模拟它

【讨论】:

  • 真的非常感谢你们的帮助!我只是想知道我是否可以问。您的方法,排序输出仅适用于每个字母,我是否正确,而我相信我需要它适用于每一行。这有多容易改变?
  • @LordFlashheart 你的问题不是很清楚。请编辑您的问题并添加示例输入和预期输出,以便我们为您提供更好的帮助。
  • @LordFlashheart 请查看我的编辑,其中第一个代码块包含您想要实现的目标
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多