【问题标题】:how to pass parameters for opening files C#如何传递打开文件的参数C#
【发布时间】:2016-08-17 13:59:34
【问题描述】:

我对 c# 非常陌生,我在试图理解这一点时遇到了问题。如何将参数传递给(或从我倾向于混淆术语)主函数以读取文本文件?我有一个 python 函数可以更好地显示我想要做什么。

def readFile(filename):
    data = []
    source = file(filename)
    for line in source:
        words = line.split()
        for word in words:
            data.append(int(word))
    source.close()
    return data

我对如何在 c# 中打开文件有基本的了解,但是在网上搜索我找不到任何可以帮助我做我想做的事情或至少可以帮助翻译的东西。这是我的基本理解:

using System;
using System.Text;
using System.IO;

public class Readingfiles {


public static void Main()
{
    StreamReader src = new StreamReader("Data.txt");

    while(!src.EndOfStream)
    {
        string line = src.ReadLine();
        Console.WriteLine(line);
    }

  }
}

请帮助,如果有帮助,我使用 sublime 文本并通过 mcs/mono 在终端上编译。

【问题讨论】:

  • 你的Main应该有输入参数 --> args[]

标签: c# python mono parameter-passing


【解决方案1】:

您应该为您的main() 输入参数args

static void Main(string[] args)

举个例子:

class Program {
    static void Main(string[] args) {
        Console.WriteLine("This is the program");
        if (args == null || args.Length == 0) {
            Console.WriteLine("This has no argument");
            Console.ReadKey();
            return;
        }
        Console.WriteLine("This has {0} arguments, the arguments are: ", args.Length);
        for (int i = 0; i < args.Length; ++i)
            Console.WriteLine(args[i]);
        Console.ReadKey();
    }
}

程序会显示你是否调用.exe 参数:

C:\Release> ConsoleApplication.exe //has no argument
C:\Release> ConsoleApplication.exe Data.txt //has one argument
C:\Release> ConsoleApplication.exe CallWith Data.txt //has two arguments
C:\Release> ConsoleApplication.exe "CallWith Data.txt" //has ONE arguments

在您的情况下,参数可能只是一个字符串,因此,将输入检查放在 args[0] 上就足够了:

public static void Main(string[] args)
{
    if (args == null || args.Length == 0) {
        Console.WriteLine("Error: please specify the file to read!");
        Console.ReadKey();
        return;
    }

    try {

        StreamReader src = new StreamReader(args[0]);

        while(!src.EndOfStream)
        {
            string line = src.ReadLine();
            Console.WriteLine(line);
        }
    } catch (Exception ex) {
        Console.WriteLine("Error while reading the file! " + ex.ToString());
    }

    Console.ReadKey();    
}

编辑:

您的最终解决方案应如下所示(只需将 Main 块放入正确的命名空间和类中)并且您应该使用所有 usings:

using System;
using System.IO;

namespace ConsoleApplication2 {
    class Program {
        public static void Main(string[] args) {
            if (args == null || args.Length == 0) {
                Console.WriteLine("Error: please specify the file to read!");
                Console.ReadKey();
                return;
            }

            try {

                StreamReader src = new StreamReader(args[0]);

                while (!src.EndOfStream) {
                    string line = src.ReadLine();
                    Console.WriteLine(line);
                }
            } catch (Exception ex) {
                Console.WriteLine("Error while reading the file! " + ex.ToString());
            }

            Console.ReadKey();
        }
    }
}

【讨论】:

  • 这很好,我认为这正是我需要的,但是,打开文本文件的函数将不得不将文本文件中的信息用于类中的其他函数,如果我在 main 中调用文本打开文件会以同样的方式完成吗?
  • 如果您需要存储其他功能的信息,我建议您再做两件事:(1)创建List&lt;string&gt; textsstring[] texts以获取输入文件中的文本并使用@ 987654333@ 在所有其他功能中。 (2)在你的主函数(while(true))中放置无限while循环,这样在你专门调用Application Exit之前它不会关闭@
  • 所以我复制了你的第二个例子,它说“意外符号`文件结尾'”但文件结尾不在你的代码中。
  • 错误出现在哪一行?我只给出主要代码的例子。您需要在文件的标题部分包含一些using...只要您拥有所有库,就可以了。此外,完整文件应包含命名空间和类名。这是您已经为您的应用所做的:using System; using System.Text; using System.IO; public class Readingfiles
  • 发现问题错过了一个括号,我是新手
【解决方案2】:

您可以使用从程序的任何位置访问命令行参数

string[] args = Environment.GetCommandLineArgs();

另外,读取文件中所有行的最简单方法是

if (args.Length > 1)    //the 1st is always the path to app
    string[] lines = System.IO.File.ReadAllLines(ards[1]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2013-05-18
    相关资源
    最近更新 更多