Colonel Panic
Colonel Panic

Reputation: 137682

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

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 .

Upvotes: 7

Views: 18390

Answers (4)

Rosue Walford
Rosue Walford

Reputation: 67

Try this:

public void Main(string[] args)
        {
            if (args.Count() > 0)
            {

                byte[] byteArray = Encoding.UTF8.GetBytes(args[0]);
                MemoryStream stream = new MemoryStream(byteArray);

                StreamReader sr = new StreamReader(stream);
                String line = sr.ReadToEnd();
                Console.WriteLine(line);
            }
            Console.ReadLine();
        }

args[0] is a string which must be converted to a stream before passing to StreamReader constructor.

Upvotes: -1

Jeff Mercado
Jeff Mercado

Reputation: 134521

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);
    }
}

Upvotes: 10

Thomas
Thomas

Reputation: 1563

use

 static void Main(string[] args)

and then iterate over each input with args.length in a for-loop for example.

example of use: http://www.dotnetperls.com/main

Upvotes: 0

alvonellos
alvonellos

Reputation: 1062

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!

Upvotes: 3

Related Questions