从命令行读取

时间:2017-11-13 20:58:15

标签: c# file command-line arguments

我在阅读日期时遇到问题。任务:

目标是编写一个能够为特定文件构造和打印霍夫曼树的程序。程序将以单个命令行参数的形式接收文件的名称,从输入文件中读取所有数据并为此文件构建Huffman树...

示例:

$>program.exe simple.in

我的解决方案:

string FileName = Console.ReadLine();
fileBytes = File.ReadAllBytes(FileName);

但是这个解决方案写道:

  

RE:运行时错误102:取消引用空值

谢谢

2 个答案:

答案 0 :(得分:0)

这应该让您从命令行开始阅读,但是您需要对您尝试做的其余事情进行一些研究,并在您拥有特定的问题:

private static void Main(string[] args)
{
    Console.WriteLine($"The command line arguments entered are: {string.Join(" ", args)}");

    if (args.Length > 0)
    {
        var filePath = args[0];

        if (!File.Exists(filePath))
        {
            Console.WriteLine($"The specified file was not found: {filePath}");
        }
        else
        {
            // Process the file here
            var fileBytes = File.ReadAllBytes(filePath);
        }
    }
    else
    {
        Console.WriteLine(
            "Error: Enter the full path to a file as the first command line argument");
    }


    Console.WriteLine("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

答案 1 :(得分:0)

我解决了。 而不是:

Console.ReadLine();

我用过:

args[0];