Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read from file in command-line arguments otherwise standard in? (emulate Python's fileinput)

Tags:

c#

.net

I'd like my app to read from files specified by command-line argument or from standard in, so the user can use it myprogram.exe data.txt or otherprogram.exe | myprogram.exe. How can I do this in C#?


In Python, I'd write

import fileinput
for line in fileinput.input():
    process(line)

This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-', it is also replaced by sys.stdin.

Perl's <> and Ruby's ARGF are similarly useful .

like image 217
Colonel Panic Avatar asked Oct 07 '12 18:10

Colonel Panic


People also ask

How read a file from command line in Python?

To read a text file in Python, you follow these steps: First, open a text file for reading by using the open() function. Second, read text from the text file using the file read() , readline() , or readlines() method of the file object. Third, close the file using the file close() method.

How do you pass a command line argument to a Python program?

In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys. argv ) will store all the information in the command line entry and can be accessed inside the Python script. Python's getopt module can also be used to parse named arguments.


2 Answers

stdin is exposed to you as a TextReader through Console.In. Just declare a TextReader variable for your input that either uses Console.In or the file of your choosing and use that for all your input operations.

static TextReader input = Console.In;
static void Main(string[] args)
{
    if (args.Any())
    {
        var path = args[0];
        if (File.Exists(path))
        {
            input = File.OpenText(path);
        }
    }

    // use `input` for all input operations
    for (string line; (line = input.ReadLine()) != null; )
    {
        Console.WriteLine(line);
    }
}

Otherwise if refactoring to use this new variable would be too expensive, you could always redirect Console.In to your file using Console.SetIn().

static void Main(string[] args)
{
    if (args.Any())
    {
        var path = args[0];
        if (File.Exists(path))
        {
            Console.SetIn(File.OpenText(path));
        }
    }

    // Just use the console like normal
    for (string line; (line = Console.ReadLine()) != null; )
    {
        Console.WriteLine(line);
    }
}
like image 176
Jeff Mercado Avatar answered Oct 26 '22 18:10

Jeff Mercado


That's awfully easy, actually.

In the C# code editor, you can do:

public static void Main(string[] args) {
    //And then you open up a file. 
    using(Streamreader sr = new Streamreader(args[0])) {
            String line = sr.ReadToEnd();
            Console.WriteLine(line);
    }
}

Another good idea would be to iterate over the items args in a c# collection, so that you can take multiple files as input. Example: main.exe file1.txt file2.txt file3.txt and so on.

You'd do that by modifying the above code using a special for loop, like follows:

foreach(string s in args) {
    using( Streamreader sr = new Streamreader(s) ) {
        String line = sr.ReadToEnd();
        Console.WriteLine(line);
    }
}

Good luck!

like image 3
alvonellos Avatar answered Oct 26 '22 19:10

alvonellos